结束在SwitchCase内部使用空行(Enter键)循环

时间:2015-06-20 02:43:38

标签: 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);
    }

然而,当我将它集成到我的主程序中时(同时循环放置在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

3 个答案:

答案 0 :(得分:0)

我不是百分百肯定,但我认为扫描仪没有读取所有输入。当扫描仪开始读取时,缓冲区中有一个空行(换行符),因此它返回一个空行,这会导致行长度为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 );
   }
}

及其输出:

run:
Choose a function (-1 to exit): 1
..1
Choose a function (-1 to exit): 2
..2
>><<
Choose a function (-1 to exit): -1
BUILD SUCCESSFUL (total time: 11 seconds)

您可以看到该行打印为>><<,这意味着它已被读取但为空。我认为上面的'2'左边有换行符,因为换行符不是数字2的一部分,而是留在缓冲区中。

答案 1 :(得分:0)

扫描仪无法读取任何输入,因为当前行中没有令牌(按2后)。

在阅读输入之前,您需要使用scan.nextLine()(忽略剩余的currrent线并将扫描仪移动到下一行的开头)。

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

希望它对你有所帮助。

答案 2 :(得分:0)

感谢您的帮助!经过一些试验和错误,我得到了代码才能工作

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(),
的干扰 只是我的理论