gpt4 book ai didi

Java 测验 - 正确时不接受用户输入

转载 作者:行者123 更新时间:2023-12-01 21:10:34 30 4
gpt4 key购买 nike



private void startGame() {
System.out.println("Exercises must be completed as quickly as possible.");
System.out.println("You have to solve 3 exercises.");
System.out.println("Be as fast as you can!\n");

Instant startTime = Instant.now();

for (int i = 0; i < 3; i++) {
Exercise exercise = Exercise.getRandomExercise();
System.out.print(exercise);
int guess = Integer.parseInt(scanner.nextLine());
exercise.solve(guess);
if (!exercise.solve(guess)) {
System.out.println("\nYou have made a mistake.");
System.out.println("More luck next time.\n");
return;
}
}
System.out.println();

Instant endTime = Instant.now();
Duration timeElapsed = Duration.between(startTime, endTime);
double seconds = timeElapsed.toMillis() / 1000.0;

Record record = new Record(seconds);
System.out.println("You needed " + seconds + " seconds.");
if (recordList.add(record)) {
System.out.println("You have set a new record!");
System.out.print("Your name: ");
record.setName(scanner.nextLine());
}
System.out.println();
}


}



import java.util.Random;

class Exercise {
private String question;
private String solution;

private Exercise(String question, String solution) {
this.question = question;
this.solution = solution;
}


public static Exercise getRandomExercise() {

records s1 = new records();
records s2 = new records();
records s3 = new records();

// RECORDS OF QUESTIONS
s1 = setQuestion(s1, "What is the largest bone in the human body? \n Choose 1 for Femur \n Choose 2 for Tibia \n Choose 3 for Palatine Bone \n Choose 4 for Tongue \n");
s2 = setQuestion(s2, "What is the capital of Albania? \n Choose 1 for Shkoder \n Choose 2 for Tirana \n Choose 3 for Durres \n Choose 4 for Rome \n");
s3 = setQuestion(s3, "The beaver is the national emblem of which country? \n Choose 1 for England \n Choose 2 for America \n Choose 3 for Canada \n Choose 4 for Texas \n");

// RECORDS OF ANSWERS
s1 = setCorrectAnswer(s1, 1);
s2 = setCorrectAnswer(s2, 2);
s3 = setCorrectAnswer(s3, 3);

// ARRAY CONTAINING THE QUESTION WITH THE CORRESPONDING CORRECT ANSWER
String[][] questions = {
{getQuestion(s1), getCorrectAnswer(s1)},
{getQuestion(s2), getCorrectAnswer(s2)},
{getQuestion(s3), getCorrectAnswer(s3)}};


Random random = new Random();
int index = random.nextInt(5);
String question;
String solution;

if (index == 0) {
System.out.println(questions[0][0]);
question = questions[0][0];
solution = questions[0][1];

} else if (index == 1) {
System.out.println(questions[1][0]);
question = questions[1][0];
solution = questions[1][1];

} else {
System.out.println(questions[2][0]);
question = questions[2][0];
solution = questions[2][1];
}
return new Exercise(question, solution);
}

public boolean solve(int guess) {
return solution.equals(guess);
}


private static String getQuestion(records s) {
return s.question;
}

public static String getCorrectAnswer(records s) {
return String.valueOf(s.correctanswer);
}


// SETTER METHODS
public static records setQuestion(records s, String question) {
s.question = question;
return s;
}

public static records setCorrectAnswer(records s, int correctanswer) {
s.correctanswer = correctanswer;
return s;
}



}

我创建了一个数组,其中包含包含问题和相应正确答案的记录。运行以下代码时,如果用户输入正确的答案,则视为用户输入了错误的答案。我希望程序能够看到用户已正确输入答案

非常感谢

最佳答案

您在Exercise 的solve 方法中比较了String 和Integer。这永远不会返回 true,因为两个对象的类型不同

关于Java 测验 - 正确时不接受用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904812/

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