Java 在SwitchCase内以空行(回车键)结束While循环
*修改输入扫描Java 在SwitchCase内以空行(回车键)结束While循环,java,while-loop,switch-statement,blank-line,Java,While Loop,Switch Statement,Blank Line,*修改输入扫描 我试图用回车键/空行结束while循环 下面的测试代码可以正常工作 Scanner scan = new Scanner(System.in); String line; while ( (line=scan.nextLine()).length() > 0) { System.out.println(line); } 但是,当我将其集成到主程序中时(while循环放在do-while循环的int-switch
我试图用回车键/空行结束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
我不是100%确定,但我认为扫描仪没有读取所有输入。当扫描仪开始读取时,缓冲区中有一个空行(换行符),因此它返回一个空行,这会导致行长为0,并立即退出循环 以下是我对您当前计划的最佳猜测:
public class InputTest
{
public static void main( String[] args )
{
Scanner scan = new Scanner( System.in );
int input = 0;
do {
System.out.print( "Choose a function (-1 to exit): " );
input = scan.nextInt();
switch( input ) {
case 1:
System.out.println("..1");
break;
case 2:
System.out.println("..2");
//same while loop here
String line = scan.nextLine();
System.out.println(">>"+line+"<<");
while( line.length() > 0 ) {
System.out.println( " Read line: " + line );
line = scan.nextLine();
System.out.println(">>"+line+"<<");
}
break;
}
} while( input != -1 );
}
}
公共类输入测试
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int输入=0;
做{
系统输出打印(“选择一个函数(-1)退出):”;
输入=scan.nextInt();
开关(输入){
案例1:
系统输出打印项次(“…1”);
打破
案例2:
系统输出打印项次(“…2”);
//这里也是
String line=scan.nextLine();
System.out.println(“>>”+line+“>”+line+“扫描仪无法读取任何输入,因为当前行中没有令牌(按2键后)
在读取输入之前,需要使用scan.nextLine()(忽略当前行的其余部分并将扫描仪移动到下一行的开头)
do{
System.out.print("Choose a function (-1 to exit): ");
input=scan.nextInt();
switch (input)
{
case 1:
break;
case 2:
scan.nextLine();//skips the entire current line.
while ( (line=scan.nextLine()).length() > 0)
{
System.out.println(line);
}
//System.out.println(line.length());
break;
}
}while(input!=-1);
输出
Choose a function (-1 to exit): 2
hello
hello
bye
bye
Choose a function (-1 to exit):-1
希望它能对您有所帮助。谢谢您的帮助!经过一些尝试和错误后,我让代码正常工作了
Scanner scan = new Scanner(System.in);
int input;
do{
System.out.print("Choose a function (-1 to exit): ");
input=scan.nextInt();
switch (input)
{
case 1:
break;
case 2:
System.out.println("..2");
String line;
scan.nextLine();//skips the entire current line.
while ( (line=scan.nextLine()).length() > 0)
{
System.out.println(line);
}
break;
}
}while(input!=-1);
System.out.println()似乎清除了scan.nextInt()的干扰,
虽然只是我的理论,但你能发布你的整个方法体吗?谢谢。是的,特别是在分配了输入
的情况下。你没有在开关盒内接受任何输入。这将变成无限循环,而循环应以分号结束
问题在于扫描仪,因为在你输入后,当前行中没有标记ed 2.这就是为什么您需要跳过当前行并移动到下一行的开头来读取输入。
Scanner scan = new Scanner(System.in);
int input;
do{
System.out.print("Choose a function (-1 to exit): ");
input=scan.nextInt();
switch (input)
{
case 1:
break;
case 2:
System.out.println("..2");
String line;
scan.nextLine();//skips the entire current line.
while ( (line=scan.nextLine()).length() > 0)
{
System.out.println(line);
}
break;
}
}while(input!=-1);