Java的!用户完成回答问题后如何显示正确和错误的答案问题

时间:2016-06-22 11:49:27

标签: java

我有一个Java程序,询问用户问题并总计他们的分数。我试图在程序结束时显示正确的已回答问题总数。但是,我不知道怎么样,可以请一些帮助我! 这是我的代码看起来像l的一个例子!但这不是我的实际代码,而是来自其他来源!

Import java.util.Scanner;


public class App
{
    public static void main(String[] args) {

        // The array of questions.
        String questions[] = {
                "Plants derive most of their dry mass from the air.",
                "Aluminium is the most common metal in the Earth's crust.",
                "Vitamin C has be shown to prevent colds.",
                "We lose the most heat through our heads.",
                "Dogs are unable to digest chocolate.",
                "Apple pips contain cyanide.",
                "Cholesterol is a nat
                "When you're on a diet, you lose weight by oxidising fat to a gas and exhaling it.",
                "Human beings are unable to sense when the oxygen level of the air is low.",
                "Most of the Y chromosome is passed unchanged from father to son" };

        // The array of answers. The entries correspond to the questions.
        boolean answers[] = { true, true, false, false, false, true, false,
                              true, true, true };

        // Display the opening text.
        System.out.println("Answer each of the following questions with 't' (true) or 'f' (false)");

        // We'll use this to get user input.
        Scanner input = new Scanner(System.in);

        // Add up the user's score here as we go along.
        int score = 0;

        // The is the index into the questions array and the answers array.
        int questionNumber = 0;

        // Create a blank line.
        System.out.println();

        // The do-while loop will keep running while questionNumber is less
        // than the question array length.
        do {
            // Display the question
            System.out.println(questions[questionNumber]);

            // Display a little prompt to make it clearer that the user has to
            // enter something.
            System.out.print("> ");

            // Get the user's answer.
            String userAnswer = input.nextLine();

            // Check that the user has entered t or f.
            if (!userAnswer.equals("t") && !userAnswer.equals("f")) {
                System.out.println("Please enter t for true or f for false.\n");

                // Invalid input!
                // Skip the rest of this loop iteration and ask the same question again.
                continue;
            }

            // Check the answer.
            if (userAnswer.equals("t") && answers[questionNumber] == true) {
                // If the answer's t and the right answer is "true", the answer was correct.
                score++;
                System.out.println("correct\n");
            } else if (userAnswer.equals("f") && answers[questionNumber] == false) {
                // If the answer's f and the correct answer is "false", the answer was correct.
                System.out.println("correct\n");
                score++;
            }
            else {
                // Wrong answer!
                System.out.println("incorrect!\n");
            }

            // Now we can move to the next question when we go round the loop again.
            questionNumber++;

        } while (questionNumber < questions.length); // end of do-while.

        // This isn't really necessary, but closing the Scanner prevents a warning icon in Eclipse.
        input.close();

        // Tell the user their score.
        System.out.println("You scored: " + score);

        // Rank the score! Only one of the alternatives below will execute.
        // Java will check them in order from top to bottom.
        if(score < 5) {
            // Less than 5 -- not so good.
            System.out.println("Hmmm, maybe you're the artistic type. Try the test again!");
        }
        else if(score < 8) {
            // The score wasn't less than 5, but it IS less than 8.
            System.out.println("Not bad! But have another go.");
        }
        else if(score <= 9) {
            // The score wasn't less than 8, but it IS less than, or equal to, 9.
            System.out.println("Pretty good! But no perfect. Try again!");
        }
        else {
            // The score was more than 9 -- must be 10 because we've only got 10 questions.
            System.out.println("You're a certified science genius!");
        }

    }
}

2 个答案:

答案 0 :(得分:0)

只需在类中声明一个包含用户答案的​​布尔数组:

public boolean[] userAnswers = new boolean[questions.length]; //remove the access modifier if you want to declare the array inside main instead of the class

现在在do-while循环中,在解析用户输入后,执行以下操作:

userAnswers[questionNumber] = userAnswer.equals("t");

在你的do-while循环之外(关闭Scanner之后),执行以下操作以打印所有问题和用户的答案:

for (int i = 0; i < questions.length; i++) {
    System.out.println("User answered \"" + userAnswers[i] + "\" on question " + (i + 1) + ": " + questions[i]);
}

答案 1 :(得分:0)

当用户正确回答问题时,您可以使用列表并添加问题nummer。 在执行do-while循环之前,您必须添加

.QDockWidget{
    DockWidgetClosable: true;
}

然后在你的do-while循环中,你必须在答案正确时添加

ArrayList<Integer> correctQuestions = new ArrayList<Integer>();

在程序结束时(在do-while-loop之外)你可以像这样输出:

correctQuestions.add(questionNumber);