dimanche 13 septembre 2020

What is wrong with this java while loop?

New to Java and learning how to use While loops and random generator. This prints a multiplication question. Every time the user answers a question wrong, it should print the same question. Instead, it exits the program. What should I do?

while (true) {
    Random multiply = new Random();
    int num1 = multiply.nextInt(15);
    int num2 = multiply.nextInt(15);
    int output = num1 * num2;

    System.out.println("What is the answer to " + num1 + " * " + num2);

    Scanner input = new Scanner(System.in);
    int answer = input.nextInt();
    if (answer == output) {
        if (answer != -1)
            System.out.println("Very good!");
    } else {
        System.out.println("That is incorrect, please try again.");
    }
}



Aucun commentaire:

Enregistrer un commentaire