User entering a loop until conditions are met

I need to ask the user to enter a number that will be used as the beginning of the range, and then enter another number that is the end of the range. The start number must be 0 or greater, and the end number cannot be greater than 1000. Both numbers must be divisible by 10. I found a way to fulfill these conditions, however, if they are not met, my program simply tells the user that their input was incorrect . Is it possible for me to encode it so that after user input, it checks to make sure that the conditions are met, and if they do not go back and enter them again. Here is the code I have.

Scanner keyboard = new Scanner(System.in); int startr; int endr; System.out.println("Enter the Starting Number of the Range: "); startr=keyboard.nextInt(); if(startr%10==0&&startr>=0){ System.out.println("Enter the Ending Number of the Range: "); endr=keyboard.nextInt(); if(endr%10==0&&endr<=1000){ }else{ System.out.println("Numbers is not divisible by 10"); } }else{ System.out.println("Numbers is not divisible by 10"); } 
+4
source share
3 answers

Easy with do-while:

 Scanner keyboard = new Scanner(System.in); int startr, endr; boolean good = false; do { System.out.println("Enter the Starting Number of the Range: "); startr = keyboard.nextInt(); if(startr % 10 == 0 && startr >= 0) good = true; else System.out.println("Numbers is not divisible by 10"); } while (!good); good = false; do { System.out.println("Enter the Ending Number of the Range: "); endr = keyboard.nextInt(); if(endr % 10 == 0 && endr <= 1000) good = true; else System.out.println("Numbers is not divisible by 10"); } while (!good); // do stuff 
+6
source

You need to use some time, something like:

 while conditionsMet is false // gather input and verify if user input valid then conditionsMet = true; end loop 

must do it.

+3
source

Universal procedure:

  • Read the input in an infinite loop.
  • Use break; to exit the loop when conditions are met.

Example:

 Scanner keyboard = new Scanner(System.in); int startr, endr; for (;;) { System.out.println("Enter the starting number of the range: "); startr = keyboard.nextInt(); if (startr >= 0 && startr % 10 == 0) break; System.out.println("Number must be >= 0 and divisible by 10."); } for (;;) { System.out.println("Enter the ending number of the range: "); endr = keyboard.nextInt(); if (endr <= 1000 && endr % 10 == 0) break; System.out.println("Number must be <= 1000 and divisible by 10."); } 

If, after an invalid input, you only want to display an error message without repeating the initial prompt, move the initial prompt directly above / outside the loop.

If you do not need a separate error message, you can re-arrange the code to use the do-while loop to check for conditions that are slightly shorter:

 Scanner keyboard = new Scanner(System.in); int startr, endr; do { System.out.println("Enter the starting number of the range."); System.out.println("Number must be >= 0 and divisible by 10: "); startr = keyboard.nextInt(); } while (!(startr >= 0 && startr % 10 == 0)); do { System.out.println("Enter the ending number of the range."); System.out.println("Number must be <= 1000 and divisible by 10: "); endr = keyboard.nextInt(); } while (!(endr <= 1000 && endr % 10 == 0)); 
0
source

All Articles