*修正的输入扫描
我正在尝试用输入键/空白行结束一个while循环下面的测试代码工作正常。
Scanner scan = new Scanner(System.in);
String line;
while ( (line=scan.nextLine()).length() > 0)
{
System.out.println(line);
}
但是,当我将它集成到我的主程序中时(while 循环放置在 do-while 循环内的 int switch case 中),程序会跳过代码 @case2 并继续 do-while 循环
int input;
do{
System.out.print("Choose a function (-1 to exit): ");
input=scan.nextInt();
switch (input)
{
case 1:
break;
case 2:
//same while loop here
break;
}
}while(input!=-1);
样本输出:
Choose a function (-1 to exit): 1
Choose a function (-1 to exit): 2
//skip case2 coding
Choose a function (-1 to exit): 1
Choose a function (-1 to exit): -1
//program ends