我正在尝试创建一个随机的数学测验程序(数字应该在0和20之间)。
然而,当给出正确的答案时,程序就会终止。我该如何解决这个问题呢?
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 + "%!");
}
}
}
}
}发布于 2015-09-17 10:35:15
您可以通过不像现在这样嵌套if语句来修复它。这就是你要做的:
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
}
}这就是你想要做的:
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不相关的
如果你想要两个介于0和20之间的随机整数,你应该这样做:
Random rnd = new Random();
int randomNumber1 = rnd.nextInt(21); // 0-20
int randomNumber2 = rnd.nextInt(21); // 0-20当然,您可能不希望randomNumber2为0 (除以零错误),因此1-20对于randomNumber2可能更好
Random rnd = new Random();
int randomNumber1 = rnd.nextInt(21); // 0-20
int randomNumber2 = rnd.nextInt(20) + 1; // 1-20发布于 2015-09-17 10:42:03
我假设您希望程序继续运行,直到用户决定存在。
一种天真的方法是将"Welcome“行之后的所有内容放入while循环中,该循环只有在用户输入终止值时才会终止,比如"exit”或"e“作为他们的答案。
// 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);发布于 2015-09-17 10:55:09
非常感谢!我刚刚解决了这个问题。如果我问了一些非常基本的问题,我道歉!
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 + "%!");
}
}https://stackoverflow.com/questions/32621678
复制相似问题