0

*修正的输入扫描
我正在尝试用输入键/空白行结束一个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
4

3 回答 3

0

我不是 100% 确定,但我认为扫描仪没有读取所有输入。当 Scanner 开始读取时,缓冲区中有一个空行(换行符),因此它返回一个空行,这导致您的行长度为 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的一部分,并且会留在缓冲区中。

于 2015-06-20T02:58:41.860 回答
0

扫描仪无法读取任何输入,因为当前行中没有令牌(按 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

希望它可以帮助你。

于 2015-06-20T03:06:42.017 回答
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()
的干扰,但这只是我的理论

于 2015-06-20T12:58:04.563 回答