为什么我的 while 循环只对某些嵌套的"if"语句有效,而对其他语句无效?



while循环应该询问用户他们想选择哪个选项,在选项完成后,它应该回到while循环的开始。现在,它只对第二个else if正确工作,而不是其他。

while(flag){
System.out.println("Select an option from the menu below:" + 
"n1: Display consecutive numbers in a right triangle" + //needs better explanation
"n2: Determine if a number is divisible by 3" + 
"n3: Determine the number of periods and spaces in a string" +
"n4: Display a right triangle of astrisks" + 
"n5: Exit"); //better explanation
String option = input.nextLine();
if(option.equals("1")){  //does not work
System.out.println("Enter number of columns: ");
int width = input.nextInt(); 
while(width<=0 || width%1!=0){
System.out.println("Invalid input! Please enter a positive width.");
width = input.nextInt(); 
}
numOfColumns(width);
}
else if(option.equals("2")){ //does not work
System.out.println("Enter number:");
int num = input.nextInt();
divisibleBy3(Math.abs(num));
}
else if(option.equals("3")){ //works
System.out.println("Enter string:");
String sentence = input.nextLine();
System.out.println("Number of periods and spaces: " + periodsAndSpaces(sentence) + "n");
}

else if(option.equals("4")){ //does not work
System.out.println("Enter the width of the triangle:");
int length = input.nextInt();
rightTriangle(length, "");
}
else if(option.equals("5")){ //exits the while loop
flag = false;
}
else{
System.out.println("That is not a valid option!");
}
}

对于上面指示的if/else if语句,程序将执行给定的方法,然后显示菜单,后面跟着"这不是一个有效的输入!",然后再次显示菜单,即使用户没有输入任何内容。

正如在注释中提到的,在使用input.nextInt();或任何其他不消耗整行的方法之后,您需要消耗该行的其余部分,因为它只接受该行的一部分输入,其余部分留在扫描仪上。您可以通过input.nextLine();来修复此问题,以便在while循环重新启动时为下一个输入做好准备,例如:

//Get a partial line input eg "nextInt()"
int someInput = input.nextInt();
//Consume the rest of the line
input.nextLine();
//Now process the input
...

下面是使用上述技术的有效解决方案:

if(option.equals("1")){  //does not work
System.out.println("Enter number of columns: ");
int width = input.nextInt(); 
//Consume the rest of the line
input.nextLine();
//Now process the input
while(width<=0 || width%1!=0){
System.out.println("Invalid input! Please enter a positive width.");
width = input.nextInt(); 
}
numOfColumns(width);
}
else if(option.equals("2")){ //does not work
System.out.println("Enter number:");
int num = input.nextInt();
//Consume the rest of the line
input.nextLine();
//Now process the input
divisibleBy3(Math.abs(num));
}
else if(option.equals("3")){ //works
System.out.println("Enter string:");
String sentence = input.nextLine();
System.out.println("Number of periods and spaces: " + periodsAndSpaces(sentence) + "n");
}

else if(option.equals("4")){ //does not work
System.out.println("Enter the width of the triangle:");
int length = input.nextInt();
//Consume the rest of the line
input.nextLine();
//Now process the input
rightTriangle(length, "");
}
else if(option.equals("5")){ //exits the while loop
flag = false;
}
else{
System.out.println("That is not a valid option!");
}

允许每个选项正确工作。

我推断你的input变量是一个Scanner对象。

选项3是唯一一个按预期工作的选项,因为它是唯一一个对特定选项的输入使用nextLine()的选项,从输入缓冲区中清除输入末尾的换行符。

如果有人选择选项1,然后输入3,结果如下:

  1. input包含字符串"3n"
  2. nextInt()读取3
  3. input中仍然有尚未读取的"n"
  4. numOfColumns(3)runs
  5. 循环的下一次迭代开始
  6. 输出菜单
  7. nextLine()input读取,立即找到步骤3中仍然存在的n,清除该字符,并返回一个空字符串
  8. option""的值通过无效输入
  9. 循环重新开始
  10. 输出菜单
  11. nextLine()等待新的输入内容再次可用

要获得您期望的行为,在每个input.nextInt()调用之后调用input.nextLine()

最新更新