英文:
Is there a way to have the user input their answer immediately after the question in this array?
问题
// 程序开始
public class MathQuiz {
public static void main(String[] args) {
// 变量
int correctAnswers = 0;
int incorrectAnswers = 0;
double percCorrect = 0;
final int numberOfQuestions = 10;
double[] userGuesses = new double[numberOfQuestions];
// 问题数组
String[] questions = new String[10];
questions[0] = "Question 1: (4 – 5) – (13 – 18 + 2) = ?";
// 其他问题省略...
// 答案数组
double[] answers = {2, 4, 3.9, 13, 10, 0.4, 13, 5, 1, 3};
// 输入输出问题和用户答案
for (int i = 0; i < numberOfQuestions; i++) {
System.out.println(questions[i]);
System.out.print("Your guess: ");
userGuesses[i] = keyedInput.nextDouble(); // 假设你有一个 Scanner 变量 named keyedInput
}
// 检查答案并计算结果
for (int i = 0; i < numberOfQuestions; i++) {
if (userGuesses[i] == answers[i]) {
correctAnswers++;
} else {
incorrectAnswers++;
}
}
// 计算百分比
percCorrect = ((double) correctAnswers / numberOfQuestions) * 100;
// 输出结果
System.out.println("Results:");
System.out.println("Correct Answers: " + correctAnswers);
System.out.println("Incorrect Answers: " + incorrectAnswers);
System.out.println("Correct Percentage: " + percCorrect + "%");
}
}
// 程序结束
以上是你的代码的翻译。如果你有更多的问题或需要进一步的帮助,请随时提问。
英文:
I'm new to java and I'm supposed to code something with arrays and loops, so I'm having 10 math questions that the user will try to answer. The program then outputs the number of questions they got correct and incorrect, and also the correct percentage. For the loop part I'm planning to add a play again later. This is what I have so far...
// 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();
This goes on till question 10 but I wanted to know if there is a way I can have the user input their answer immediately after each question is printed. It would be like:
// Question
// User's guess
// Next question
// User's guess
At the end of the 10 questions, the program would output the results and I would have if statements to check if they are correct for each question and calculate the correct percentage and so on.
I don't know how to go about checking the user's answer against the correct answer and counting the correct answers, incorrect answers and calculating the average in a way that's less tedious than what I have so far. I want to follow the question, user's guess, question, user's guess format because I feel like it would be simpler. Like I said, I've only just started java so any help or tips would be appreciated. Please let me know if there is anything else I should fix. Thank you!
答案1
得分: 2
你可以对答案做类似于问题的操作(将它们添加到一个数组中),从而将它们合并在一起,就像这样:
//...
String userGuess[]= new String[10];
//...
// 答案数组
String [ ] answers = {"2", "4", "3.9", "13", "10", "0.4", "13", "5", "1", "3"}; // answers = 数组名称,包含 10 个字符串值(答案)
// 输出问题
for (int i = 0; i <= 9; i = i + 1)
{
System.out.println(questions[i]); // 输出问题,例如当 i = 0 时,系统输出 questions[0]
System.out.print("Input your answer for Question " + (i+1) + ":");
userGuess[i] = keyedInput.nextLine();
if(userGuess[i].equals(answers[i]))
correctAnswers++;
else
incorrectAnswers++;
} // 循环结束
percCorrect = (correctAnswers * 1.0 / numberOfQuestions) * 100;
System.out.println(percCorrect);
英文:
You could simply do the same thing for the answers(adding them in an array) as for the questions and therefore uniting them, like this:
//...
double userGuess[]= new double[10];
//...
for (int i = 0; i <= 9; i = i + 1)
{
System.out.println(questions[i]); // outputting questions, eg when i = 0, system outputs questions[0]
System.out.print("Input your answer for Question "+(i+1)+":");
userGuess[i] = keyedInput.nextDouble();
} // end of for loop
However, given you are planning to check if the doubles from userGuess are equal to the doubles from the answers, I'd suggest actually using Strings as opposed to doubles, as doubles could possibly have floating point errors and you don't want the inaccuracy.
Also, in order to ensure for the percent part that you can get intermediary values (since / between two integers is integer division, returning the quotient!) multiply one of the elements divided by 1.0 before the division, in order to turn it into float and therefore get a normal division that returns floating point.
...
String userGuess[]= new String[10];
...
// array for answers
String [ ] 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]
System.out.print("Input your answer for Question "+(i+1)+":");
userGuess[i] = keyedInput.nextLine();
if(userGuess[i].equals(answers[i]))
correctAnswers++;
else
incorrectAnswers++;
} // end of for loop
percCorrect=(correctAnswers*1.0/numberOfQuestions)*100;
System.out.println(percCorrect);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论