Java 如何借助try-catch语句生成一个循环,命令用户在满足条件之前一直输入一个值?
我试图告诉用户继续输入正确的值格式,直到它正确完成。我想向用户显示,如果他/她输入了错误的格式(值的数量),他/她应该重试,系统将要求用户输入新值。我如何使用下面的代码做到这一点 底部的while语句是否有效(正确编写)?比如,如果没有触发异常,停止“do:ing” 另外,我知道我下面的代码看起来很糟糕,因为我只是一个初学者,不知道如何正确格式化代码Java 如何借助try-catch语句生成一个循环,命令用户在满足条件之前一直输入一个值?,java,Java,我试图告诉用户继续输入正确的值格式,直到它正确完成。我想向用户显示,如果他/她输入了错误的格式(值的数量),他/她应该重试,系统将要求用户输入新值。我如何使用下面的代码做到这一点 底部的while语句是否有效(正确编写)?比如,如果没有触发异常,停止“do:ing” 另外,我知道我下面的代码看起来很糟糕,因为我只是一个初学者,不知道如何正确格式化代码 public class PersonTidbok { public static void main(String[] args){
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);
}
}
按如下方式操作:
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.");
}
}
}
运行示例:
Welcome to your interface, please select of the following (P, T, L, S, A, Q): a
Wrong value for choice.
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...
另一个示例运行:
Welcome to your interface, please select of the following (P, T, L, S, A, Q): a
Wrong value for choice.
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...
这回答了你的问题吗?我呼吁重新打开它,因为它不仅涉及循环,直到用户做出正确的输入,还涉及正确的格式以及如何抛出/捕获异常。哇,两件事,我关闭了这个线程,因为我相信它会被否决,然后我可能会禁止lol。其次,我不能再发布问题了:C我不知道是否可以重新打开此线程。@ArvindKumarAvinash:每个问题对于初学者来说都是独一无二的,我很高兴你帮助了OP。不幸的是,堆栈溢出将充满“为什么我的==不比较字符串?”,“为什么我的扫描仪跳过用户输入?”,“我如何检查用户输入的错误?”,等等“我不知道所有的老师和学校的服务台都到哪里去了,但它们似乎都不见了。”@GilbertLeBlanc——我完全同意。谢谢你鼓励我的话。我可以问你一个离题的问题吗?你是否知道有什么工具或程序可以帮助我在“布局”、“外观”、“设计”方面正确地格式化代码?@Zerinity你要找的是一个IDE软件。它们是非常有用和强大的。VSCode和Sublime很流行。对于Java,请尝试名为intelliJ IDEA的IDE,并使用菜单项Code>Reformat Code来调整代码的视觉布局。