我是Java的新手,应该使用数组和循环编写代码,因此我遇到了10个数学问题,用户将尝试回答。然后,程序将输出正确和不正确的问题数量以及正确的百分比。对于循环部分,我计划稍后再添加一个播放。这是我到目前为止所拥有的...
// variables
int correctAnswers = 0;
int incorrectAnswers = 0;
double percCorrect= 0;
final int numberOfQuestions = 10;
double userGuess1;
double userGuess2;
double userGuess3;
double userGuess4;
double userGuess5;
double userGuess6;
double userGuess7;
double userGuess8;
double userGuess9;
double userGuess10;
// intro
System.out.println("Enter your answer to the following math questions to test your knowledge!");
System.out.println();
// array for questions
String [ ] questions = new String [10]; // questions = name of array, holds 10 string values (questions)
// assigning questions
questions[0] = "Question 1: (4 – 5) – (13 – 18 + 2) = ?";
questions[1] = "Question 2: What number is next in this sequence: 31, 30, 22, 21, 13, 12, __";
questions[2] = "Question 3: Round 3.864 to the nearest tenth";
questions[3] = "Question 4: If 3x = 6x - 15, then x + 8 = ?";
questions[4] = "Question 5: The value of x + x(x^x) when x = 2 is ?";
questions[5] = "Question 6: 20% of 2 = ?";
questions[6] = "Question 7: What number is next in this sequence: 0, 1, 1, 2, 3, 5, 8, __";
questions[7] = "Question 8: Two sides of a right angled triangle are 3 and 4. The third side is ?";
questions[8] = "Question 9: What's the degree of 7x + 5 ?";
questions[9] = "Question 10: Solve for x: 5x + 2(x + 7) = 14x – 7";
// array for answers
double [ ] answers = {2, 4, 3.9, 13, 10, 0.4, 13, 5, 1, 3}; // answers = name of array, holds 10 double values (answers)
// outputting questions
for (int i = 0; i <= 9; i = i + 1)
{
System.out.println(questions[i]); // outputting questions, eg when i = 0, system outputs questions[0]
} // end of for loop
// user inputs their answers
System.out.println();
System.out.print("Input your answer for Question 1: ");
userGuess1 = keyedInput.nextDouble();
System.out.print("Input your answer for Question 2: ");
userGuess2 = keyedInput.nextDouble();
System.out.print("Input your answer for Question 3: ");
userGuess3 = keyedInput.nextDouble();
这一直持续到问题10,但我想知道是否有一种方法可以让用户在每个问题打印后立即输入答案。就像:
// Question
// User's guess
// Next question
// User's guess
在10个问题的末尾,程序将输出结果,我将拥有if语句来检查它们是否对每个问题正确并计算正确的百分比,依此类推。
我不知道如何对照正确答案检查用户的答案,并计算正确答案,错误答案以及以比我到目前为止没有那么繁琐的方式计算平均值。我想遵循问题,用户的猜测,问题,用户的猜测格式,因为我觉得这会更简单。就像我说的,我只是刚刚启动Java,所以任何帮助或提示都将不胜感激。请让我知道是否还有其他应解决的问题。谢谢!