How to use a Do-while loop that continuously prompts a user?

(I have a homework question that I’ve been stuck on that concerns “do-while loops” in Java. )

It is asking me to have a do-while loop that continues to prompt a user to enter a “number less than 100”, up until the entered number is actually less than 100.

(It will run three tests:) Ex: Test 1: For the user input 123, 395, 25, the expected output is:

Enter a number (<100): 
Enter a number (<100): 
Enter a number (<100): 
Your number < 100 is: 25

(Here’s my code so far:)

public class NumberPrompt {
   public static void main (String [] args) {
   Scanner scnr = new Scanner(System.in);
   int userInput = 0;

      do {
         System.out.println("Enter a number (<100):" );
         System.out.println("Enter a number (<100):" );
         System.out.println("Enter a number (<100):" );
         userInput = userInput + 25;
      } while (userInput > 100);
         System.out.print("");

      System.out.println("Your number < 100 is: " + userInput);
}

(My Output matches exactly with the Test 1 results above, but I realize I’m not setting up the loop right at all because when it does the second test of “-9”, the output exactly the same as my first test:)

Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25

(This is my first week being introduced to loops, I searched around for some walk through examples but I haven’t found many “Do” while loops that remind me of this one. If anyone has some good tips or guides to point me to I would greatly appreciate it.)

Leave a Comment