简体   繁体   中英

Loop switch code doesn't repeat but executes other case

I have a switch case code that displays a menu of four choices. Once a choice and it's action is complete, it displays the menu again. The last case (fourth choice) in the menu is to quit. The code works perfectly until I get to the end of the case 3, but then it doesn't display the menu again and just jumps straight to executing case 4 and terminates the program.

(FYI, I am very new at this, so thank you for your patience).

Here is the code:

do
{
   // Display the menu
   displayMenu();

   // Ask the user for one option
   System.out.println("Enter a number: ");

   switch (scan.nextInt()) 
   {
       // Define four cases for different options. Don't forget "break".
   case 1:
       System.out.print("Enter a number: ");
       sumInput = scan.nextInt();
       while (sumStart <= sumInput)
       {
           sumResult += sumStart;
           sumStart++;
       }
       System.out.println("The sum of 1 to " + sumInput + " is: " + sumResult);
       break;
   
   case 2:
       System.out.print("Enter a number: ");
       facInput = scan.nextLong();
       while(facStart <= facInput)
       {            
           facResult = facResult*facStart;
           facStart++;
       }         
       System.out.printf("%s %.0f %s %.0f %n", "The factorial of ",  facInput , " is ",  facResult);
       break;
                        
   case 3:
       System.out.print("Enter a number: ");
       int left = scan.nextInt();
       while(left >= 1)
       {               
           String leftString = String.valueOf(left);
       System.out.print("The leftmost digit of " + left + " is ");
       System.out.println(leftString.charAt(0));
       break;
       }

   case 4:
       System.out.print("Bye");
       System.exit(0);
   }
}
while (option < 4);

I think in case 3 you've given break statement in your while loop Try to remove break from while loop and add it after the loop

case 3:
               System.out.print("Enter a number: ");
               int left = scan.nextInt();
               while(left >= 1)
               {               
                   String leftString = String.valueOf(left);
               System.out.print("The leftmost digit of " + left + " is ");
               System.out.println(leftString.charAt(0));
               };break;

You want to take the break statement out of your while loop so it doesn't break immediately:

case 3:
     System.out.print("Enter a number: ");
     int left = scan.nextInt();
     while(left >= 1)
     {               
          String leftString = String.valueOf(left);
          System.out.print("The leftmost digit of " + left + " is ");
          System.out.println(leftString.charAt(0));
     }
     break;

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