How do I make a loop that commands the user to keep inputing a value until If conditions are met, with the aid of a try catch statement?

huangapple go评论77阅读模式
英文:

How do I make a loop that commands the user to keep inputing a value until If conditions are met, with the aid of a try catch statement?

问题

我试图告诉用户保持输入正确的值格式,直到格式正确为止。我想向用户显示,如果他/她输入了错误的格式(值的数量),他/她应该重试,系统将要求用户输入新的值。我如何使用下面的代码实现这一点?

并且底部的 while 语句是否有效(写得正确)?就像如果异常没有被触发,停止 "do:ing"。

注:我知道我的代码看起来很糟糕,因为我是一个纯初学者,不知道如何正确格式化代码。

public class PersonTidbok {

   public static void main(String[] args){

      Scanner console = new Scanner(System.in);
      char choice;
      
      System.out.print("欢迎来到您的界面,请从以下选项中选择:\n" +
                        "P, T, L, S, A, Q"); 

      choice = console.next().charAt(0);

      switch (choice){
         case 'P':
            do{ 
               System.out.print("请输入格式为 (YYYYMMDD) 的 persnr:");
               try{
                  String persnr = console.next();
                  if (persnr.length() != 8) {
                     throw new FormatException();
                  }
               }
               catch (FormatException exception){
                  System.out.println(exception + " 您输入了错误的格式,请重试。"); 
               }
            } while (true); // 此处应为循环条件
            // 其他选项的处理...
      }
   }
}

P.S. 请注意,我已经重新格式化了您的代码,并做了一些修改以使其正确运行。在你的代码中,有一些小错误,我已经帮你进行了修正。

英文:

Im trying to tell the user to keep inputing the right value format until its done properly. I want to display to the user that if he/she has typed in the wrong format (number of values), he/she should try again and the system will ask the user to input new values. How do I do that with the following code below?

And is the while statement at the bottom valid (properly written)? Like if the exception isnt triggered, stop "do:ing"

P.S, I know that my code below looks awful, as I'm a mere beginner and do not know how to format code properly

public class PersonTidbok {

   public static void main(String[] args){


      Scanner console = new Scanner (System.in);
      
      System.out.print ("Welcome to your interface, please select of the following: " +
                        "\nP, T, L, S, A, Q"); 
    
  choice = console.next().charAt(0);
      
      switch (choice){
     
 case P:
      
      do{ 
      
      System.out.print (" enter persnr in the format of (YYYYMMDD));
     
        try{
        
        persnr = console.nextInt();
   
           if ( persnr.length != 8);
       
             throw new FormatException();
      }
                     
                catch (FormatException exception){
                System.out.println(exception + "You printed wrong format, try again")); 
   }
}
   while (!FormatException);
}
}

答案1

得分: 1

import java.util.Scanner;

public class PersonTidbok {
    public static void main(String[] argv) {
        Scanner console = new Scanner(System.in);
        boolean valid;
        char choice = '\0';
        String persnr;
        do {
            valid = true;
            System.out.print("欢迎访问您的界面,请选择以下操作之一(P,T,L,S,A,Q):");
            String input = console.nextLine();
            if (input.length() != 1) {
                System.out.println("无效输入,请重试。");
                valid = false;
            }
            choice = input.charAt(0);
        } while (!valid);

        switch (choice) {
            case 'P':
                do {
                    valid = true;
                    System.out.print("请输入格式为(YYYYMMDD)的身份证号码:");
                    try {
                        persnr = console.nextLine();
                        if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                            throw new IllegalArgumentException("您输入的格式有误,请重试。");
                        }
                        System.out.println("正在处理...");
                        // ...身份证号码处理应在此处进行
                    } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage());
                        valid = false;
                    }
                } while (!valid);
                break;
            default:
                System.out.println("选择的值错误。");
        }
    }
}

示例运行:

欢迎访问您的界面,请选择以下操作之一(P,T,L,S,A,Q):a
选择的值错误。

另一个示例运行:

欢迎访问您的界面,请选择以下操作之一(P,T,L,S,A,Q):PT
无效输入,请重试。
欢迎访问您的界面,请选择以下操作之一(P,T,L,S,A,Q):P
请输入格式为(YYYYMMDD)的身份证号码:01234567
您输入的格式有误,请重试。
请输入格式为(YYYYMMDD)的身份证号码:ancdefgh
您输入的格式有误,请重试。
请输入格式为(YYYYMMDD)的身份证号码:20180912
正在处理...
英文:

Do it as follows:

import java.util.Scanner;
public class PersonTidbok {
public static void main(String[] argv) {
Scanner console = new Scanner(System.in);
boolean valid;
char choice = '\0';
String persnr;
do {
valid = true;
System.out.print("Welcome to your interface, please select of the following (P, T, L, S, A, Q): ");
String input = console.nextLine();
if (input.length() != 1) {
System.out.println("Invalid input. Try again.");
valid = false;
}
choice = input.charAt(0);
} while (!valid);
switch (choice) {
case 'P':
do {
valid = true;
System.out.print("Enter persnr in the format of (YYYYMMDD): ");
try {
persnr = console.nextLine();
if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
throw new IllegalArgumentException("You printed wrong format, try again");
}
System.out.println("Processsing...");
// ...Processing of persnr should go here
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
valid = false;
}
} while (!valid);
break;
default:
System.out.println("Wrong value for choice.");
}
}
}

A sample run:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): a
Wrong value for choice.

Another sample run:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): PT
Invalid input. Try again.
Welcome to your interface, please select of the following (P, T, L, S, A, Q): P
Enter persnr in the format of (YYYYMMDD): 01234567
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): ancdefgh
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): 20180912
Processsing...

huangapple
  • 本文由 发表于 2020年3月16日 05:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/60698078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定