简体   繁体   English

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

[英]Java Quiz - not accepting users input when correct



    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;
    }



}

I've created an array with the records containing the question with the corresponding correct answer.我创建了一个数组,其中包含包含问题和相应正确答案的记录。 When running the following code, if the user enters the correct answer, it takes it as if the user entered the wrong answer.运行以下代码时,如果用户输入正确答案,则将其视为用户输入错误答案。 I want the program to be able to see that the user has entered the answer correctly我希望程序能够看到用户已正确输入答案

Many thanks非常感谢

You compare String with Integer in solve method of Exercise.您在练习的求解方法中将字符串与 Integer 进行比较。 This can never return true because the type of the two objects are different这永远不会返回 true,因为两个对象的类型不同

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

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