简体   繁体   English

我如何跟踪正确答案?

[英]How do i keep track of correct answers?

public static void main(String[] args) {
    Scanner Keyboard = new Scanner(System.in);
    System.out.println("Enter your name: ");
    String firstname =Keyboard.nextLine();
    System.out.println("Welcome "+ firstname+  "!"+ " Please answer the following questions:");
    int x =  (int)(20 * Math.random()) + 1;
    int y =  (int)(20 * Math.random()) + 1;

    int sum = (x+y);
    System.out.println(x + " + " + y + " = ");
    String sInput = Keyboard.nextLine();
    int answer1 = Integer.parseInt(sInput);
    if (answer1 ==sum){
        System.out.println("Correct!");
    }else{
    System.out.println("Wrong!");
    }
    System.out.println("The correct answer is " +sum);

I have no clue on how to keep track of the correct answers.我不知道如何跟踪正确答案。 I need something to keep track of when it prints correct.我需要一些东西来跟踪打印正确的时间。 I don't know what to do though.我不知道该怎么做。 I know I just need to record the corrects and divide by four.我知道我只需要记录正确并除以四。 Four because thats how many questions I have in my quiz.四,因为这就是我的测验中有多少问题。

If you just need to keep track of how many right answers were provided, just add an int variable starting with 0 as a value and increment it.如果您只需要跟踪提供了多少正确答案,只需添加一个从 0 开始的 int 变量作为值并递增它。 If you want to keep track of the questions and answers which were right, create an empty ArrayList and add a string every time a correct answer is provided.如果您想跟踪正确的问题和答案,请创建一个空的 ArrayList 并在每次提供正确答案时添加一个字符串。

Here an example of the second option:这是第二个选项的示例:

    ArrayList<String> correctAnswers = new ArrayList<String>();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter your name: ");
    String firstname =keyboard.nextLine();
    System.out.println("Welcome "+ firstname+  "!"+ " Please answer the following questions:");
    for (int i=0;i<10;i++) {
        int x =  (int)(20 * Math.random()) + 1;
        int y =  (int)(20 * Math.random()) + 1;

        int sum = (x+y);
        System.out.println(x + " + " + y + " = ");
        String sInput = keyboard.nextLine();
        int answer1 = Integer.parseInt(sInput);
        if (answer1 ==sum){
            System.out.println("Correct!");
            correctAnswers.add(x + " + " + y + " = " + sum);
        }
        else{
            System.out.println("Wrong!");
            System.out.println("The correct answer is " +sum);     
        }
    }
    System.out.println("Correct answers:");
    for (String correctAnswer : correctAnswers) {
        System.out.println(correctAnswer);
    }

It asks 10 questions, keeps track of the right answers and outputs them after the 10th question.它提出 10 个问题,跟踪正确答案并在第 10 个问题之后输出它们。

An example for the first option:第一个选项的示例:

    int correctAnswers=0;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter your name: ");
    String firstname =keyboard.nextLine();
    System.out.println("Welcome "+ firstname+  "!"+ " Please answer the following questions:");
    int totalAnswers=4;
    for (int i=0;i<totalAnswers;i++) {
        int x =  (int)(20 * Math.random()) + 1;
        int y =  (int)(20 * Math.random()) + 1;

        int sum = (x+y);
        System.out.println(x + " + " + y + " = ");
        String sInput = keyboard.nextLine();
        int answer1 = Integer.parseInt(sInput);
        if (answer1 ==sum){
            System.out.println("Correct!");
            correctAnswers++;
        }
        else{
            System.out.println("Wrong!");
            System.out.println("The correct answer is " +sum);     
        }
    }
    System.out.println("Correct answers: "+ correctAnswers + "("+correctAnswers*100/totalAnswers+"%)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM