This program must randomly choose a response whenever the answer is correct. and will ask the same question again if the answer is wrong.
My code is something like this.
Scanner reader = new Scanner(System.in);
int response = 0;
int answer = 0;
while (true) {
int num1 = (int) (Math.random() * 10 +1);
int num2 = (int) (Math.random() * 10 + 1);
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println("What is " + num1 + " times " + num2 + "?");
answer = reader.nextInt();
switch(response) {
case 0:
if (answer != (num1 * num2)) {
System.out.println("No. Please try again.");
System.out.println("What is " + num1 + " times " + num2 + "?");
answer = reader.nextInt();
}
if (answer == (num1 * num2)) {
System.out.println("Very good.");
}
case 1:
if (answer != (num1 * num2)) {
System.out.println("No. Please try again.");
System.out.println("What is " + num1 + " times " + num2 + "?");
answer = reader.nextInt();
}
if (answer == (num1 * num2)) {
System.out.println("Very good.");
}
and the task was "Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses."
The problem in the code that I have tried is that it doesn't randomly choose a response on my switch case statement, any ideas?
Aucun commentaire:
Enregistrer un commentaire