java - 为什么我的 while 循环只对某些嵌套的“if”语句有效,而对其他语句无效?
问题描述
while 循环应该询问用户他们想要选择哪个选项,并且在选项完成后它应该返回到 while 循环的开始。现在,它只适用于 second 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!");
}
这允许每个选项正常工作。
推荐阅读
- go - 这个示例客户端的 Go GRPC 服务器应该如何?
- oauth-2.0 - Google 操作和 Auth0 帐户关联令牌每 24 小时过期一次
- python - readthedocs 上的 nbsphinx 语法高亮显示
- ruby-on-rails - Rails – 嵌套资源 – 我的编辑和删除方法检索到错误的 ID
- laravel - 脚本@php artisan package:discover --ansi 处理返回的自动加载转储后事件,错误代码为 255
- python - TypeError:“查询”对象在 Odoo 10 中不可迭代
- javascript - 当数据源更改时,d3 图形在 DOM 上重复
- .net - 使用 dot net core 无服务器应用程序从/向 kafka AWS MSK 生成和使用消息的问题
- c++ - 3D 到 2D 世界对象坐标变换
- java - 电子邮件方法的错误是什么?