[英]Random Math Quiz program with Java
我正在尝试创建一个随机数学测验程序(数字应该在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 + "%!");
}
}
}
}
}
您可以通过不像您一样嵌套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
我假设您希望程序继续运行,直到用户决定存在为止。
天真的方法是将“欢迎”行之后的所有内容都放入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);
非常感谢! 我只是解决了这个问题。 如果我问的是非常基本的问题,我深表歉意!
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.