Welcome to Westonci.ca, where finding answers to your questions is made simple by our community of experts. Join our Q&A platform and get accurate answers to all your questions from professionals across multiple disciplines. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.
Sagot :
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.
We appreciate your time on our site. Don't hesitate to return whenever you have more questions or need further clarification. We hope our answers were useful. Return anytime for more information and answers to any other questions you have. Westonci.ca is your go-to source for reliable answers. Return soon for more expert insights.