有没有办法让用户在这个数组中的问题之后立即输入他们的答案?

huangapple go评论84阅读模式
英文:

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(&quot;Enter your answer to the following math questions to test your knowledge!&quot;);
System.out.println();
// array for questions
String [ ] questions = new String [10]; // questions = name of array, holds 10 string values (questions)
// assigning questions
questions[0] = &quot;Question 1: (4 – 5) – (13 – 18 + 2) = ?&quot;;
questions[1] = &quot;Question 2: What number is next in this sequence: 31, 30, 22, 21, 13, 12, __&quot;;
questions[2] = &quot;Question 3: Round 3.864 to the nearest tenth&quot;;
questions[3] = &quot;Question 4: If 3x = 6x - 15, then x + 8 = ?&quot;;
questions[4] = &quot;Question 5: The value of x + x(x^x) when x = 2 is ?&quot;;
questions[5] = &quot;Question 6: 20% of 2 = ?&quot;;
questions[6] = &quot;Question 7: What number is next in this sequence: 0, 1, 1, 2, 3, 5, 8, __&quot;;
questions[7] = &quot;Question 8: Two sides of a right angled triangle are 3 and 4. The third side is ?&quot;;
questions[8] = &quot;Question 9: What&#39;s the degree of 7x + 5 ?&quot;;
questions[9] = &quot;Question 10: Solve for x: 5x + 2(x + 7) = 14x – 7&quot;;
// 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 &lt;= 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(&quot;Input your answer for Question 1: &quot;);
userGuess1 = keyedInput.nextDouble();
System.out.print(&quot;Input your answer for Question 2: &quot;);
userGuess2 = keyedInput.nextDouble();
System.out.print(&quot;Input your answer for Question 3: &quot;);
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&#39;s guess
// Next question
// User&#39;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 &lt;= 9; i = i + 1)
{
    System.out.println(questions[i]); // outputting questions, eg when i = 0, system outputs questions[0]
    System.out.print(&quot;Input your answer for Question &quot;+(i+1)+&quot;:&quot;);
    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 = {&quot;2&quot;, &quot;4&quot;, &quot;3.9&quot;, &quot;13&quot;, &quot;10&quot;, &quot;0.4&quot;, &quot;13&quot;, &quot;5&quot;, &quot;1&quot;, &quot;3&quot;}; // answers = name of array, holds 10 double values (answers)

// outputting questions
for (int i = 0; i &lt;= 9; i = i + 1)
{
    System.out.println(questions[i]); // outputting questions, eg when i = 0, system outputs questions[0]
    System.out.print(&quot;Input your answer for Question &quot;+(i+1)+&quot;:&quot;);
    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);

huangapple
  • 本文由 发表于 2020年4月9日 04:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定