简体   繁体   中英

How do I stop a loop from indefinitely looping in Java?

I have this try/catch wrapped around a do/while loop because after the try/catch throws the error message, I want it to loop back to the top. I tried do/while, while, and I tried placing the while loop at different places in my code but nothing works. The program works fine until an exception is thrown and then it goes into an infinite loop. After is displays the error message, I just want it to loop back up to the top.

public static void main(String[] args) {

    Scanner input = new Scanner( System.in );

    Integer userInput;
  do {
   try{ 
    System.out.print("Enter a number? \n");
    userInput = input.nextInt();

      if ( userInput == 1 )
         Animal1.displayMessage ();//Display the total
      if( userInput == 2 )
         Animal2.displayMessage ();//Display the total

      }
       catch (Exception e) {
        System.out.println(" That's not right ");
        break;

      }
      } while (true);
      }

}

This is what it does after displaying an error message.

    Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
 That's not right 
Enter a number? 
Enter a number? 

If I don't stop it, it just keeps going.

you can try this workaround:

public static void main(String[] args) {

    Scanner input = new Scanner( System.in );

    Integer userInput;
  do {
   try{ 
    System.out.print("Enter a number? \n");
    userInput = input.nextInt();

      if ( userInput == 1 )
         Animal1.displayMessage ();//Display the total
      if( userInput == 2 )
         Animal2.displayMessage ();//Display the total

      }
       catch (Exception e) {
        System.out.println(" That's not right ");
        input.next();

      }
      } while (true);
      }

} 

or if you want to avoid try-catch:

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Integer userInput = 0;
        do {
            System.out.print("Enter a number? \n");
            if (input.hasNextInt())
                userInput = input.nextInt();
            else {
                System.out.println(" That's not right ");
                input.next();
            }
            if (userInput == 1)
                Animal1.displayMessage ();//Display the total
            ;// Display the total
            if (userInput == 2)
                Animal2.displayMessage ();//Display the total

        } while (true);
    }

You can give 3 options - one option to exit

System.out.print("Enter a number? \n 1 to display Animal1 total\n2 to display Animal2 total\n 3 to exit");

Inside while loop, you can add

if ( userInput == 3) break;

您需要将try / catch语句放在循环之外。

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