Java 我的测验可以实行计分制吗?我想在测验结束后显示5分的分数

Java 我的测验可以实行计分制吗?我想在测验结束后显示5分的分数,java,Java,这是我现在的进步。我真的非常感谢你的帮助。我这样做只是为了练习,但我真的希望这个程序尽可能的详细和简洁。我正在学习,所以我欢迎任何建议。我读到我需要解析字符串来给它们一个整数值,但我不知道如何去做 import java.util.Scanner; public class TriviaPart1 { public static void main(String[] args) { //Trivia Questions: Scanner scan = new Scanner (

这是我现在的进步。我真的非常感谢你的帮助。我这样做只是为了练习,但我真的希望这个程序尽可能的详细和简洁。我正在学习,所以我欢迎任何建议。我读到我需要解析字符串来给它们一个整数值,但我不知道如何去做

import java.util.Scanner;

public class TriviaPart1 {
public static void main(String[] args) {
    //Trivia Questions:
    Scanner scan = new Scanner (System.in);

    System.out.println("Welcome! Time to begin some trivia");
    //The program pauses for 2 seconds before asking the first question
    try {
        Thread.sleep(2000);
    }catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    //Question 1
    System.out.println("Who holds the world record for running the fastest 100 meter dash?");

    String questionOne;
    questionOne = scan.nextLine();

    if(questionOne.equalsIgnoreCase("Usain Bolt")) {
        System.out.println("Correct!");
    }
    else 
        System.out.println("Incorrect. The answer was: Usain Bolt");
        System.out.println("Next Question");

        try {
            Thread.sleep(2000);
            }catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
    //Question 2
    System.out.println("What is the World's current population?");

    String questionTwo;
    questionTwo = scan.nextLine();

    if(questionTwo.equalsIgnoreCase("7 billion")){
        System.out.println("Correct!");
    }   
    else
        System.out.println("Incorrect. The answer was: 7 billion");
        System.out.println("Next Question");

        try {
            Thread.sleep(2000);
            }catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

        //Question Three
        System.out.println("Why is the sunset blue on Mars?");

        String questionThree;
        questionThree = scan.nextLine();

        if(questionThree.equalsIgnoreCase("Distance from the sun causes the sunlight to lose intensity"))
        {
            System.out.println("Correct!");
        }   
        else
            System.out.println("Incorrect. The answer was: Distance from the sun causes the sunlight to lose intensity");
            System.out.println("Next Question");

            try {
                Thread.sleep(2000);
                }catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }

            //Question Four
            System.out.println("What mobile carrier is the most popular in the United States?");

            String questionFour;
            questionFour = scan.nextLine();

            if(questionFour.equalsIgnoreCase("Verizon Wireless")){
                System.out.println("Correct!");
            }

            if(questionFour.equals("Verizon")){
                System.out.println("Correct!");
            }
            else 
                System.out.println("Incorrect. The answer was: Verizon Wireless");
                System.out.println("Next Question");

                try {
                    Thread.sleep(2000);
                    }catch(InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }

            //Question Five
            System.out.println("What is the most popular television show today?");

                String questionFive;
                questionFive = scan.nextLine();

            if(questionFive.equalsIgnoreCase("The Big Bang Theory")){
                System.out.println("Correct!");
            }
            else
                System.out.println("Incorrect. The answer was: The Big Bang Theory");
                System.out.println("End of Trivia Part 1");

            }

}

我可以在这方面给你一些指导,但实际的实现取决于你。你可以声明一个全局变量,并在答案正确时使其递增 例如:


在此之后,您可以使用
i
的值来显示您的答案。

我可以在这方面为您提供指导,但实际实现取决于您。您可以声明一个全局变量,并在答案正确时使其递增 例如:


在此之后,您可以使用
i
的值来显示您的答案。

在程序开始时,您应该声明一个名为
correctCount
的变量,如下所示:

int correctCount = 0;
System.out.println("Your score: " + correctCount + "/5");
每次用户得到正确答案时,向变量中添加1:

//Question 1
System.out.println("Who holds the world record for running the fastest 100 meter dash?");

String questionOne;
questionOne = scan.nextLine();

if(questionOne.equalsIgnoreCase("Usain Bolt")) {
    System.out.println("Correct!");
    correctCount++; //here you increase the variable by one.
}
else 
    System.out.println("Incorrect. The answer was: Usain Bolt");
    System.out.println("Next Question");

    try {
        Thread.sleep(2000);
        }catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
现在,当节目结束时,您可以如下显示分数:

int correctCount = 0;
System.out.println("Your score: " + correctCount + "/5");

在程序开始时,您应该声明一个名为
correctCount
的变量,如下所示:

int correctCount = 0;
System.out.println("Your score: " + correctCount + "/5");
每次用户得到正确答案时,向变量中添加1:

//Question 1
System.out.println("Who holds the world record for running the fastest 100 meter dash?");

String questionOne;
questionOne = scan.nextLine();

if(questionOne.equalsIgnoreCase("Usain Bolt")) {
    System.out.println("Correct!");
    correctCount++; //here you increase the variable by one.
}
else 
    System.out.println("Incorrect. The answer was: Usain Bolt");
    System.out.println("Next Question");

    try {
        Thread.sleep(2000);
        }catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
现在,当节目结束时,您可以如下显示分数:

int correctCount = 0;
System.out.println("Your score: " + correctCount + "/5");