我正在尝试创建一个随机数学测验程序(数字应该在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 + "%!");
}
}
}
}
}
答案 0 :(得分:1)
您可以通过不像您正在执行的那样嵌套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
(除以零错误),因此对于randomNumber2
来说,1-20可能更好:
Random rnd = new Random();
int randomNumber1 = rnd.nextInt(21); // 0-20
int randomNumber2 = rnd.nextInt(20) + 1; // 1-20
答案 1 :(得分:0)
我假设您希望程序继续运行,直到用户决定存在。
一种天真的方法是将大部分内容放在"欢迎"行进入while循环,只有当用户输入终止值时才会终止,例如"退出"或者" 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);
答案 2 :(得分:0)
非常感谢你!我刚刚解决了这个问题。如果我问一些非常基本的东西,我道歉!
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 + "%!");
}
}
答案 3 :(得分:-2)
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);
}
}