java - 是否可以对我的测验实现评分系统?我希望测验结束后显示 5 分制的分数

标签 java

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

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");

            }

}

最佳答案

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

int correctCount = 0;

每次用户得到正确答案时,您就将变量加 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();
        }

现在,当程序结束时,您将像这样显示分数:

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

关于java - 是否可以对我的测验实现评分系统?我希望测验结束后显示 5 分制的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515551/

相关文章:

java - 从日期列表中获取所有缺失的日期(已排序)

java - log4j MDC可以用于非线程应用程序吗?

java - CloudSim迁移: allocation and selection policies examples在哪里

java - 从java获取所有语言

java - 类路径和可移植性

java - 如何监听不针对我的应用程序的剪贴板粘贴?

java - JUnit 中的测试数据

Java 使用单独的类移动图像

java - 运行最快的算法竞赛

java - 如何在 Spring Boot 上部署时读取静态文件