3

I just want to loop this program with the user entering "yes". I figured I would use a do/while loop around the whole program but nothing happens. Here is my code:

public static void main(String[] args) {
    int num1, i;
    Scanner Scan = new Scanner(System.in);
    do {
        do {
            System.out.print("Enter a number between 20 and 30 ---> ");
            num1 = Scan.nextInt();
        } while (num1 > 30 || num1 < 20);

        for (i = num1; i >= 20; i--) {
            System.out.print(i + " ");
        }
        System.out.println("");
    } while (Scan.equals("yes"));
}
1
  • 2
    You aren't asking the user to input YES after showing him the list of numbers, so Scan still contains the number he input. Commented Oct 17, 2015 at 18:23

1 Answer 1

3
public static void main(String[] args) {
    int num1, i;
    String choice;
    Scanner Scan = new Scanner(System.in);

    do {
        do {
            System.out.print("Enter a number between 20 and 30 ---> ");
            num1 = Scan.nextInt();
        } while (num1 > 30 || num1 < 20);

        for (i = num1; i >= 20; i--) {

            System.out.print(i + " ");
        }

        System.out.println("");
        choice = Scan.next();
    }
    while (choice.equals("yes"));
}

You need to read the choice of the user input.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.