【问题标题】:Do-while loop won't loop after switch-case statementdo-while 循环不会在 switch-case 语句之后循环
【发布时间】:2019-04-16 00:49:56
【问题描述】:

我正在尝试编写一些代码来询问用户他们想要完成什么功能(例如在图表上查找某个点是哪个象限等)。但我也希望代码要求用户重新输入一个数字,如果它不在 1 和 6 之间。我试图通过创建一个 do-while 循环来做到这一点,但由于某种原因它甚至不会循环。任何使它更短/更清洁的提示都非常感谢。

以下是我遇到的问题:

  int whichMethod;
   do{ 
     whichMethod = scan.nextInt();
     switch(whichMethod){
       case 1:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point p = new Point(x, y);
         System.out.println("Quadrant:"+ p.quadrant());
         break;
       case 2:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case2p = new Point(x, y);
         case2p.flip();
         System.out.println("Flipped Coordinates" + case2p);
         break;
       case 3:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case3p = new Point(x, y);

         System.out.println("Enter x and y values for the 2nd Point: ");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case3p2 = new Point(x, y);
         System.out.println("Manhattan Distance:"+ 
         case3p.manhattanDistance(case3p2));
         break;
       case 4:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case4p = new Point(x, y);

         System.out.println("Enter x and y values for the 2nd Point: ");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case4p2 = new Point(x, y);
         System.out.println("Are they Vertical?: " + case4p.isVertical(case4p2));
         break;
       case 5:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case5p = new Point(x, y);

         System.out.println("Enter x and y values for the 2nd Point:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case5p2 = new Point(x, y);
         System.out.println("Slope is: " + case5p.slope(case5p2));
         break;
       case 6:
         System.out.println("Enter x and y values:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case6p = new Point(x, y);

         System.out.println("Enter x and y values for the 2nd Point:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case6p2 = new Point(x, y);

         System.out.println("Enter x and y values for the 3rd Point:");
         x = scan.nextDouble();
         y = scan.nextDouble();
         Point case6p3 = new Point(x, y);
         System.out.println("Are they Collinear?: "+ case6p.isCollinear(case6p2, case6p3));
         break;
       default:
         System.out.println("This isn't one of the methods available.");
         System.out.println("Please enter a number between 1 and 6");      
      } 

} while((whichMethod >= 1) && (whichMethod <= 6));

【问题讨论】:

  • 你的 while 条件被反转了。

标签: java switch-statement do-while


【解决方案1】:

只有当用户输入一个有效的数字而不是当他们输入一个无效的数字时,你的循环才会循环。 在做这种事情时,我倾向于使用集合,所以布尔逻辑很容易,即使在 2 天不睡觉和 10 壶冷咖啡之后:)

Set<Integer> validInputs = new Set();
validInputs.add(1);
validInputs.add(2);


do {
// your stuff here

} while (!validInputs.contains(inputMethod));

如果您曾经远离基于控制台的用户交互,拥有一组有效值将有助于 Swing 和 JavaFX。

【讨论】:

    【解决方案2】:

    添加 whichMethod = scan.nextInt() 作为默认值的最后一行。如果用户输入了错误的数字,这将允许用户输入有效的数字。

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2020-05-07
      相关资源
      最近更新 更多