为什么我的第一个while循环检查用户输入的有效性,而其他两个月和年的while循环只是被跳过(忽略)?我试图让用户正确输入日期,而不是让他们输入不可能的值。
// A read() method to read in the account details from the user
boolean success = false;
public void read()
{
Scanner keyboardIn = new Scanner(System.in);
System.out.println("ENTER ACCOUNT DETAILS: ");
System.out.print("User Title: ");
String title = keyboardIn.nextLine();
name.setTitle(title);
System.out.print("User First name: ");
String firstname = keyboardIn.nextLine();
name.setFirstName(firstname);
System.out.print("User Second name: ");
String secondname = keyboardIn.nextLine();
name.setSurname(secondname);
System.out.print("Account Address: ");
address = keyboardIn.nextLine();
// To make sure day is entered correctly (1 - 31)
while(!success)
{
try
{
System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();
dateOpened.setDay(d);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
// To make sure month is entered correctly (1 - 12)
while(!success)
{
try
{
System.out.print("Enter the month the account opened: ");
int m = keyboardIn.nextInt();
dateOpened.setMonth(m);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
// To make sure year is entered correctly (< 1900 not permitted)
while(!success)
{
try
{
System.out.print("Enter the year the account opened: ");
int y = keyboardIn.nextInt();
dateOpened.setYear(y);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.print("Enter the initial balance: ");
balance = keyboardIn.nextDouble();
System.out.print("Enter the overdraft amount: ");
overdraftAmount = keyboardIn.nextDouble();
System.out.println("Account number: " + accountNo);
System.out.println();
}
由于第一个while循环的循环条件仅在success为true时终止,因此其他两个while循环的成功将为true,并且它们将永远不会运行。
我不建议使用"success"作为循环条件。你可以这样写
System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();
while(d < 1 || d>31){
System.out.print("Enter the day the account opened: ");
d = keyboardIn.nextInt();
}
//set d as day in object
这可能有两个原因,第一个是用户输入的数据不是整数,这会抛出InputMismatchException
异常或在dateOpened.setDay(d)
失败的行中,如果dateOpened为null或者如果方法setDay
失败,则不会将success变量设置为true
在第一次while循环之后,不要重置循环条件"success"。
解决这个问题的一种方法是在每次循环后重置它
另一方面,在较小的方法中实现它,并使条件变量局部化,也可以解决这个问题。public void read()
{
Scanner keyboardIn = new Scanner(System.in);
System.out.println("ENTER ACCOUNT DETAILS: ");
System.out.print("User Title: ");
String title = keyboardIn.nextLine();
name.setTitle(title);
System.out.print("User First name: ");
String firstname = keyboardIn.nextLine();
name.setFirstName(firstname);
System.out.print("User Second name: ");
String secondname = keyboardIn.nextLine();
name.setSurname(secondname);
System.out.print("Account Address: ");
address = keyboardIn.nextLine();
// To make sure day is entered correctly (1 - 31)
readDay(keyboardIn);
// To make sure month is entered correctly (1 - 12)
readMonth(keyboardIn);
// To make sure year is entered correctly (< 1900 not permitted)
readYear(keyboardIn);
System.out.print("Enter the initial balance: ");
balance = keyboardIn.nextDouble();
System.out.print("Enter the overdraft amount: ");
overdraftAmount = keyboardIn.nextDouble();
System.out.println("Account number: " + accountNo);
System.out.println();
}
private void readDay(Scanner keyboardIn){
boolean success = false;
while(!success)
{
try
{
System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();
dateOpened.setDay(d);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
keyboardIn.next();
}
}
}
private void readMonth(Scanner keyboardIn){
boolean success = false;
while(!success)
{
try
{
System.out.print("Enter the month the account opened: ");
int m = keyboardIn.nextInt();
dateOpened.setMonth(m);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
keyboardIn.next();
}
}
}
private void readMonth(Scanner keyboardIn){
boolean success = false;
while(!success)
{
try
{
System.out.print("Enter the year the account opened: ");
int y = keyboardIn.nextInt();
dateOpened.setYear(y);
success = true;
}catch(Exception e)
{
System.out.println(e.getMessage());
keyboardIn.next();
}
}
}