Java 在while循环中重复错误消息
我目前正在学习我的Java课程,并且正在练习我迄今为止所学的东西 我有以下简单的程序,我计划使用它让用户输入他的名字Java 在while循环中重复错误消息,java,loops,exception,exception-handling,Java,Loops,Exception,Exception Handling,我目前正在学习我的Java课程,并且正在练习我迄今为止所学的东西 我有以下简单的程序,我计划使用它让用户输入他的名字 import java.util.Scanner; public class Adventure { public static final int menuStars = 65; private static Scanner input = new Scanner(System.in); public static void main(Strin
import java.util.Scanner;
public class Adventure {
public static final int menuStars = 65;
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String firstName = "";
String lastName = "";
boolean validName = false;
while(!validName){
//Entering first name
System.out.println("Please enter your first name.");
try {
firstName = input.nextLine();
if(firstName.length() == 0){
throw new Exception("Please enter a first name of at least 1 character.");
}else{
//Entering last name
System.out.println("Please enter your last name.");
lastName = input.nextLine();
if(lastName.length() == 0){
throw new Exception("Please enter a last name of at least 1 character");
}else{
System.out.println("You have entered " + firstName +" " + lastName);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
//Used to terminate loop when both first & last names are valid
validName = true;
}
}
}
我想让程序在用户输入空白名称时重复错误消息,而不是从头开始重新启动整个程序
例如,当用户输入一个空白的名字时,我希望程序不断重复“请输入至少一个字符的名字”,当用户输入一个空白的姓氏时,程序不断重复“请输入至少一个字符的姓氏”,直到用户输入一个有效的名字
但是,当前当用户输入一个空白的名字或姓氏时,我的程序将从一开始就重复它自己,而不仅仅是重复错误消息
如何让程序只重复错误消息?在打印“请输入您的名字”时,使用一个布尔变量存储true。每次打印此字符串之前,请检查此变量是否为false。此外,在循环之前将其初始化为false。姓氏也是如此
if(!printed)
{
System.out.println("Please enter your first name.");
printed=true;
}
import java.util.Scanner;
public class Adventure {
public static final int menuStars = 65;
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String firstName = "";
String lastName = "";
boolean firstNameLegal = false;
boolean lastNameLegal = false;
// Entering first name
while (!firstNameLegal) {
System.out.println("Please enter your first name.");
firstName = input.nextLine();
if (!firstName.equals(" "))
firstNameLegal = true;
else
System.out.println("Please enter a first name of at least 1 character.");
}
// Entering last name
while(!lastNameLegal){
System.out.println("Please enter your last name.");
lastName = input.nextLine();
if(!lastName.equals(" "))
lastNameLegal = true;
else
System.out.println("Please enter a last name of at least 1 character.");
}
System.out.println("You have entered " + firstName +" " + lastName);
}
}
我还没有测试过它,但我猜它可能是这样的,尽管没有try/catch,但它对我来说没有任何意义,就像你在代码中使用它一样
String firstName = "";
String lastName = "";
System.out.println("Please enter your first name.");
firstName = input.nextLine();
while(firstName.length<1){
System.out.println("Please enter a first name of at least 1 character.");
firstName = input.nextLine();
}
lastName=input.nextLine();
while(firstName.length<1){
System.out.println("Please enter a last name of at least 1 character.");
lastName = input.nextLine();
}
System.out.println("You have entered " + firstName +" " + lastName);
String firstName=”“;
字符串lastName=“”;
System.out.println(“请输入您的名字”);
firstName=input.nextLine();
while(firstName.length空白实际上被视为一个字符,因此(length==0)的检查不适用于您的目的
虽然下面的代码不完整(例如:处理firstname=“foo”这一可能不需要的情况(请参见function.contains()),但它执行原始帖子要求的操作-当用户输入一个空白的名字/姓氏时,它会不断重复“请输入至少1个字符的名字/姓氏”直到用户输入有效的名/姓
if(!printed)
{
System.out.println("Please enter your first name.");
printed=true;
}
import java.util.Scanner;
public class Adventure {
public static final int menuStars = 65;
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String firstName = "";
String lastName = "";
boolean firstNameLegal = false;
boolean lastNameLegal = false;
// Entering first name
while (!firstNameLegal) {
System.out.println("Please enter your first name.");
firstName = input.nextLine();
if (!firstName.equals(" "))
firstNameLegal = true;
else
System.out.println("Please enter a first name of at least 1 character.");
}
// Entering last name
while(!lastNameLegal){
System.out.println("Please enter your last name.");
lastName = input.nextLine();
if(!lastName.equals(" "))
lastNameLegal = true;
else
System.out.println("Please enter a last name of at least 1 character.");
}
System.out.println("You have entered " + firstName +" " + lastName);
}
}
为什么要使用try/catch?您是想使用它们还是只想看看它们是如何工作的?如果我理解正确,您的continue会将您带到while循环的开始,这正是您想要的。通常在这样的程序中,正确的方法是重复这个问题,但我想如果您不想这样做,您就不能这样做将问题从循环中去掉,并使用空打印。@SkarosIlias我读到,当我有办法处理异常时,应该使用try-catch循环。因此,我不应该使用try-catch循环,因为我计划重复这个问题吗?我读到,当我有办法处理异常时,应该使用try-catch循环。因此,s我是否应该在计划重复这个问题时使用try-catch循环?如果不是,我应该如何在任何代码段中使用try/catch?