Respuesta :

Answer:

import java.util.Scanner;

public class MultiplicationTablePractice {

   public static void main(String[] args) {

       var scanner = new Scanner(System.in);

       var quit = false;

       while (!quit) {

           var randomNumber1 = (int) (Math.random() * 12) + 1;

           var randomNumber2 = (int) (Math.random() * 12) + 1;

           System.out.println(randomNumber1 + " * " + randomNumber2 + " = ?");

           var answer = scanner.nextInt();

           if (answer == randomNumber1 * randomNumber2) {

               System.out.println("Correct!");

           } else {

               System.out.println("Wrong!");

           }

           System.out.println("Quit? (y/n)");

           var quitInput = scanner.next();

           if (quitInput.equals("y") ||

               quitInput.equals("Y")) {

               quit = true;

           }

       }

       scanner.close();

   }

}

Explanation:

First, we create a new scanner object, based on the System.in stream. Then we create a varaible called quit which we set to false which we then use as the condition for the while loop.

Inside the scope of the while loop, we create two random numbers based off of Math.random(). The formula we use is (int) (Math.random() * 12) + 1; With these new variables, we print:

randomNumber1 + " * " + randomNumber2 + " = ? "

After this is printed, we scan the user input as an int.  We check the input answer against the actual answer to check if it's correct or not. If it is correct, we print "Correct!" otherwise we print "Wrong!".

Once all of this is handled, we ask the user if they want to quit or not. If the input is "y" or "Y" the while loop terminates, and the scanner closes. Otherwise, the while loop will continue, and restart the process.