简体   繁体   中英

Do-while loop won't loop after switch-case statement

I'm trying to make some code that will ask the user what function they want to complete (such as finding which quadrant on a graph a point is, etc.). But I also want the code to ask the user to reenter a number if it isn't between 1 and 6 inclusive. I tried to do this by creating a do-while loop but for some reason it won't even loop. Any tips on making it shorter/cleaner are well appreciated.

Below is what I am having trouble with:

  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));

You have your loop looping only if the user enters a VALID number instead of when they enter an invalid number. When doing this sort of thing, I tend to use sets so the Boolean logic is easy, even after 2 days of no sleep and 10 pots of cold coffee :)

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


do {
// your stuff here

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

If you ever move away from a console-based user interaction, having a set of valid values will help in both swing and JavaFX.

Add whichMethod = scan.nextInt() as the last line in the default. This will allow the user to enter the valid number if user had input incorrect number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM