我正在尝试做一个数学测试类型程序
程序规范如下。
为每个操作生成2个随机数(用户选择1个用于加法,2个用于减法,3个用于乘法,4个用于除法,5个用于模数)并且你必须这样做 同样的操作至少3次
每个案例都应该跟踪并显示问题,答案, 得分和时间。
在每次操作中,当答案正确时,只需说“正确”。对于不正确的答案,应该回答问题并提供正确的答案 答案。
应保持正确的操作,问题的数量,问题本身和测试时间。
当完成每个操作的所有3个问题时,用用户的答案显示问题,表明正确或不正确 答案,然后显示问题总数,正确答案, 错误的答案,测试时间。
- 醇>
现在询问用户是否想要再次播放,即使用确认框重新开始播放。
在程序结束时,需要以下显示:
问题总数(来自所有运营),
总的正确和错误的问题数量(不是问题本身),
- 醇>
从开始到结束的总测试时间。
现在,我已经编写了整个程序。我遇到的唯一问题是通过多次试验保持正确答案的数量。就像我可以跟踪每一轮的问题一样,但我无法弄清楚如何在上一轮中添加一轮“正确答案计数”。
我需要使用方法,因此代码设置为
然后询问您是否要重复整个过程。
除非能够通过多轮(试验)跟踪正确答案,否则我完美无缺。
以下是我的主要方法:
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int correct = 0;
int option = 0;
int wrong = 0;
int count = 0;
int num1 = 0;
int num2 = 0;
int selection = 0;
do {
switch (select(selection)) {
case 1:
addDigits(num1, num2, correct);
count = count + 3;
break;
case 2:
subDigits(num1, num2, correct);
count = count + 3;
break;
case 3:
multDigits(num1, num2, correct);
count = count + 3;
break;
case 4:
divDigits(num1, num2, correct);
count = count + 3;
break;
case 5:
modDigits(num1, num2, correct);
count = count + 3;
break;
default:System.out.print("Error: invalid entries\t" + count);
}
option = JOptionPane.showConfirmDialog(null, "Continue?");
}
while (option == JOptionPane.YES_OPTION);
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
wrong = count - correct;
JOptionPane.showMessageDialog(null, "The total number of questions is: " + count +
"\nThe total number of correct answers is: " + "?????" +
"\nThe total number of incorrect answers is: " + "?????" +
"\nThe total amount of time it took to take the test is: " + (testTime /1000) + " seconds");
System.out.print(correct); //just a test
}
这是我的方法之一(它们基本相同):
public static void addDigits(int intA, int intB, int correctQuizAnswers) {
int quizCount = 0;
String output = " ";
long startQuizTime = System.currentTimeMillis();
long endQuizTime;
long quizTime;
final int NUMBER_OF_QUESTIONS = 3;
while (quizCount < NUMBER_OF_QUESTIONS){
intA = (int)(Math.random() * 10);
intB = (int)(Math.random() * 10);
String answerString = JOptionPane.showInputDialog (
"what is " + intA + " + " + intB + "?");
int answer = Integer.parseInt(answerString);
quizCount++;
if (answer == intA + intB) {
JOptionPane.showMessageDialog(null, "correct!");
correctQuizAnswers++;
}
else {
JOptionPane.showMessageDialog(null,
"Wrong, you entered: \n " + intA + " + " + intB + " = " + answerString + "\nThe correct answer is: \n" + intA + " + " + intB + " = " + (intA + intB));
}
output += "\n" + intA + " + " + intB + " = " + answerString +
((intA + intB == answer) ? " correct" : " wrong");
}
endQuizTime = System.currentTimeMillis();
quizTime = endQuizTime - startQuizTime;
JOptionPane.showMessageDialog(null, "the number of correct answers is: " + correctQuizAnswers +
"\nthe time it took to take the quiz is: " + quizTime / 1000 + " seconds\n" +
output);
}
答案 0 :(得分:-2)
在类声明(main方法所在的位置)下声明类变量:
public class myQuiz {
int totalCorrect = 0;
int totalIncorrect = 0;
每次试验结束后,请致电:
totalCorrect += correctQuizAnswers;
totalIncorrect += incorrectQuizAnswers;
上一次试验的值不正确和正确。