Charles Mosley
Charles Mosley

Reputation: 45

Looping a program with user input in Java

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"));
}

Upvotes: 3

Views: 78

Answers (1)

andreicovaciu
andreicovaciu

Reputation: 346

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.

Upvotes: 3

Related Questions