我正在尝试制作一个随机生成两位数字的程序,创建一个乘法问题,询问用户答案,计算问题数量和正确答案,并在每个问题之后用户能够停止循环看看有多少他从给出的问题数量中得出了正确的答案。
我相信我应该使用while循环(可能是最简单的方法),但我不确切知道如何去做。这是我现在拥有的:
int number = 0;
int number1 = (int)(Math.random() * 10); //Produce two single digit numbers
int number2 = (int)(Math.random() * 10);
Scanner input = new Scanner(System.in); //Create Scanner
System.out.print("What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
while (number <= 10) {
if (number1 + number2 != answer ) {
System.out.println(" Incorrect!\n Would you like to see your overall result? (yes or no)");
}
else {
System.out.println(" Correct, great job!\n Would you like to see your overall result? (yes or no)");
}
}
答案 0 :(得分:0)
由于这似乎是一项任务,我只会给你一个伪代码
MainMethod
{
//Create a new Random to generate random numbers
//(Import java.util.random)
//You need variables for:
//Total number of questions asked
//Total number of correct answers
//Start the question loop
while(true){
//Generate your two random numbers
//Ask the question
//Index the total number of questions
//Get their answer
//Check their answer
if(answer is correct){
//Index correct answers
}
//Ask whether they want to continue
//Check whether they want to stop
if(they want to quit){
//Break out of the while loop (break;)
}
}
//Tell them how many they got correct out of the total
}
也许我给了一点太多,但我希望这会有所帮助。