在 while 循环中重复错误消息
Repeating error message inside a while loop
我目前正在学习 Udemy 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(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;
}
}
}
我想让程序在用户输入空白名称时重复错误消息,而不是从头重新启动整个程序。
例如,当用户输入一个空白的名字时,我希望程序继续重复 "Please enter a first name of at least 1 character" 并且当用户输入一个空白的姓氏时,它继续重复 "Please enter a last name of at least 1 character" 直到用户输入有效名称。
但是,目前当用户输入空白的名字或姓氏时,我的程序会从头开始重复,而不是只重复错误消息。
我该如何让程序只重复错误消息?
使用布尔变量,在打印 "Please enter your first name." 时存储 true。每次打印此字符串之前检查此变量是否为假。另外,在循环之前将其初始化为 false。同样的想法也适用于姓氏。
if(!printed)
{
System.out.println("Please enter your first name.");
printed=true;
}
还没有测试过,但我猜它可能是那样的,尽管没有 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);
编辑,关于异常的一些基本信息
当发生意想不到的事情并且您试图找到解决方法时,使用 try catch。例如,如果在某个时候预期有 10 个位置的数组,而正在使用一个较小的数组(比如说 4 个位置)。然后这将导致异常,导致程序在没有进一步信息的情况下终止。
使用 try catch 你可以检查问题出在哪里,并尝试通知用户做某事(如果他们可以)或以更好的方式关闭程序,例如使用 System.exit()并保存到那时为止所做的所有工作
另一个例子是,如果你要求2个数字做加法。如果用户输入字母而不是数字,则 int sum=numbA+numbB;
将抛出异常。这当然可以使用 if 来处理。但更好的是 this
空格实际上被认为是一个字符,因此 (length == 0) 的检查对您的目的不起作用。
虽然下面的代码不完整(例如:处理 firstname="foo" 的潜在不良情况(请参阅函数 .contains()),但它会执行原始 post 要求的 - 当用户输入空白 first/last 名称,它会不断重复 "Please enter a first/last name of at least 1 character" 直到用户输入有效的 first/last 名称。
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);
}
}
我目前正在学习 Udemy 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(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;
}
}
}
我想让程序在用户输入空白名称时重复错误消息,而不是从头重新启动整个程序。
例如,当用户输入一个空白的名字时,我希望程序继续重复 "Please enter a first name of at least 1 character" 并且当用户输入一个空白的姓氏时,它继续重复 "Please enter a last name of at least 1 character" 直到用户输入有效名称。
但是,目前当用户输入空白的名字或姓氏时,我的程序会从头开始重复,而不是只重复错误消息。
我该如何让程序只重复错误消息?
使用布尔变量,在打印 "Please enter your first name." 时存储 true。每次打印此字符串之前检查此变量是否为假。另外,在循环之前将其初始化为 false。同样的想法也适用于姓氏。
if(!printed)
{
System.out.println("Please enter your first name.");
printed=true;
}
还没有测试过,但我猜它可能是那样的,尽管没有 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);
编辑,关于异常的一些基本信息 当发生意想不到的事情并且您试图找到解决方法时,使用 try catch。例如,如果在某个时候预期有 10 个位置的数组,而正在使用一个较小的数组(比如说 4 个位置)。然后这将导致异常,导致程序在没有进一步信息的情况下终止。
使用 try catch 你可以检查问题出在哪里,并尝试通知用户做某事(如果他们可以)或以更好的方式关闭程序,例如使用 System.exit()并保存到那时为止所做的所有工作
另一个例子是,如果你要求2个数字做加法。如果用户输入字母而不是数字,则 int sum=numbA+numbB;
将抛出异常。这当然可以使用 if 来处理。但更好的是 this
空格实际上被认为是一个字符,因此 (length == 0) 的检查对您的目的不起作用。
虽然下面的代码不完整(例如:处理 firstname="foo" 的潜在不良情况(请参阅函数 .contains()),但它会执行原始 post 要求的 - 当用户输入空白 first/last 名称,它会不断重复 "Please enter a first/last name of at least 1 character" 直到用户输入有效的 first/last 名称。
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);
}
}