【问题标题】:Ending While loop with blank line(Enter key) inside SwitchCase在 SwitchCase 内以空行(Enter 键)结束 While 循环
【发布时间】:2023-03-15 08:25:01
【问题描述】:

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

【问题讨论】:

  • 你能发布你的整个方法体吗?谢谢。
  • 是的,尤其是在分配了input 的情况下。
  • 你没有在 switch case 中输入任何内容。这会变成无限循环
  • do while 循环应该以semicolon结束

标签: java while-loop switch-statement blank-line


【解决方案1】:

我不是 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)

您可以看到该行打印为&gt;&gt;&lt;&lt;,这意味着它已被读取但为空。我认为上面 '2' 剩下的换行符,因为换行符不是数字 2 的一部分,并且会留在缓冲区中。

【讨论】:

    【解决方案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);    
    

    System.out.println() 似乎清除了来自 scan.nextInt() 的干扰,
    虽然只是我的理论

    【讨论】:

    • 问题出在扫描仪上,因为输入 2 后当前行中没有标记。这就是为什么您需要跳过当前行并移至下一行的开头来读取输入的原因。
    【解决方案3】:

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

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2011-11-07
      • 1970-01-01
      • 2014-10-31
      • 2019-03-15
      • 2016-02-08
      相关资源
      最近更新 更多