gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:19 29 4
gpt4 key购买 nike

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

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com