I am trying to create a random math quiz program (numbers are supposed to be between 0
and 20
).
However, when the correct answer is given the program just terminates. How can I fix this?
import java.util.Scanner;
public class Project03 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name:");
String name = keyboard.nextLine();
System.out.print("Welcome " + name + "! Please answer the following questions:");
int randomNumber1 = (int)(20 * Math.random()) + 1;
int randomNumber2 = (int)(20 * Math.random()) + 1;
int randomNumberAdd = randomNumber1 + randomNumber2;
int randomNumberMul = randomNumber1 * randomNumber2;
int randomNumberDiv = randomNumber1 / randomNumber2;
int randomNumberRem = randomNumber1 % randomNumber2;
double correct = 0;
double percentageCorrect = correct * 25;
System.out.print(randomNumber1 + " + " + randomNumber2 + " = ");
int GuessRandomNumberAdd = keyboard.nextInt();
if (GuessRandomNumberAdd == randomNumber1 + randomNumber2) {
System.out.println("Correct!");
correct++;
}
else {
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberAdd);
System.out.print(randomNumber1 + " * " + randomNumber2 + " = ");
int GuessRandomNumberMul = keyboard.nextInt();
if (GuessRandomNumberMul == randomNumber1 * randomNumber2) {
System.out.println("Correct!");
correct++;
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberMul);
}
System.out.print(randomNumber1 + " / " + randomNumber2 + " = ");
int GuessRandomNumberDiv = keyboard.nextInt();
if (GuessRandomNumberDiv == randomNumber1 / randomNumber2) {
System.out.println("Correct!");
correct++;
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberMul);
System.out.print(randomNumber1 + " % " + randomNumber2 + " = ");
int GuessRandomNumberRem = keyboard.nextInt();
if (GuessRandomNumberRem == randomNumber1 % randomNumber2) {
System.out.println("Correct!");
correct++;
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberRem);
System.out.println("You got " + correct + " correct answers.");
System.out.println("That's " + percentageCorrect + "%!");
}
}
}
}
}
You fix it by not nesting your if
statements like you are doing. This is what you do:
give first quiz
if answer is correct {
print "Correct"
} else {
print "Wrong"
give second quiz
if answer is correct {
print "Correct"
} else {
print "Wrong"
// and so on
}
}
This is what you want to do:
give first quiz
if answer is correct {
print "Correct"
} else {
print "Wrong"
}
give second quiz
if answer is correct {
print "Correct"
} else {
print "Wrong"
}
// and so on
Unrelated
If you want two random integers between 0
and 20
, both inclusive, you should do it like this:
Random rnd = new Random();
int randomNumber1 = rnd.nextInt(21); // 0-20
int randomNumber2 = rnd.nextInt(21); // 0-20
Of course, you probably don't want randomNumber2
to be 0
(division by zero error), so maybe 1-20 is better for randomNumber2
:
Random rnd = new Random();
int randomNumber1 = rnd.nextInt(21); // 0-20
int randomNumber2 = rnd.nextInt(20) + 1; // 1-20
I'm assuming you want the program to continue running until the user decides to exist.
A naive approach is to put mostly everything after the "Welcome" line into a while loop that only terminates if the user enters an termination value, like "exit" or "e" as their answer.
// Enter user name
// post first question
// enter answer or "exit" to exit program
while (input != EXIT_FLAG) {
// evaluate user input, check if correct, accumulate right/wrong scores, post next question...
}
// print % correct
System.exit(1);
Thank you so much! I just fixed the problem. I apologize if I asked something very basic!
import java.util.Scanner;
public class Project03 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name:");
String name = keyboard.nextLine();
System.out.print("Welcome " + name + "! Please answer the following questions:");
int randomNumber1 = (int)(20 * Math.random()) + 1;
int randomNumber2 = (int)(20 * Math.random()) + 1;
int randomNumberAdd = randomNumber1 + randomNumber2;
int randomNumberMul = randomNumber1 * randomNumber2;
int randomNumberDiv = randomNumber1 / randomNumber2;
int randomNumberRem = randomNumber1 % randomNumber2;
int correct = 0;
System.out.print(randomNumber1 + " + " + randomNumber2 + " = ");
int GuessRandomNumberAdd = keyboard.nextInt();
if (GuessRandomNumberAdd == randomNumber1 + randomNumber2) {
System.out.println("Correct!");
correct++;
}else {
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberAdd);
}
System.out.print(randomNumber1 + " * " + randomNumber2 + " = ");
int GuessRandomNumberMul = keyboard.nextInt();
if (GuessRandomNumberMul == randomNumber1 * randomNumber2) {
System.out.println("Correct!");
correct++;
}else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberMul);
}
System.out.print(randomNumber1 + " / " + randomNumber2 + " = ");
int GuessRandomNumberDiv = keyboard.nextInt();
if (GuessRandomNumberDiv == randomNumber1 / randomNumber2) {
System.out.println("Correct!");
correct++;
}else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberMul);
}
System.out.print(randomNumber1 + " % " + randomNumber2 + " = ");
int GuessRandomNumberRem = keyboard.nextInt();
if (GuessRandomNumberRem == randomNumber1 % randomNumber2) {
System.out.println("Correct!");
correct++;
}else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberRem);
}
double percentageCorrect = correct * 25;
System.out.println("You got " + correct + " correct answers.");
System.out.println("That's " + percentageCorrect + "%!");
}
}
import java.util.Scanner;
public class Project03 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name:");
String name = keyboard.nextLine();
System.out.print("Welcome " + name + "! Please answer the following questions:");
double correct = 0;
for(int i=0; i<10; i++) {
int randomNumber1 = (int)(20 * Math.random()) + 1;
int randomNumber2 = (int)(20 * Math.random()) + 1;
int randomNumberAdd = randomNumber1 + randomNumber2;
System.out.print(randomNumber1 + " + " + randomNumber2 + " = ");
int GuessRandomNumberAdd = keyboard.nextInt();
if (GuessRandomNumberAdd == randomNumber1 + randomNumber2)
{
System.out.println("Correct!");
correct++;
}
else
{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberAdd);
System.out.print(randomNumber1 + " + " + randomNumber2 + " = ");
int GuessRandomNumberMul = keyboard.nextInt();
if (GuessRandomNumberMul == randomNumber1 + randomNumber2) {
System.out.println("Correct!");
correct++;
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " + randomNumberAdd);
}
}
}
double percentageCorrect = (correct * 100)/10;
System.out.println("The percentage is " + percentageCorrect);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.