You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java测验程序逻辑错误排查:需实现每题对错判定及结果统计

Hey there! Let's work through fixing the logic errors in your quiz program. Since you mentioned there are no syntax issues, I’ll walk through the most common missteps that break the core functionality of showing per-question results and tallying final scores, plus share a fixed version you can use.

Common Logic Errors to Look For

Here are the top culprits that usually cause this kind of issue:

  • Using == instead of equals() for string comparisons: In Java, == checks if two string references point to the same object, not if their content matches. For comparing answer text, always use equals() (or equalsIgnoreCase() if you want to ignore case, like accepting "yes" instead of "Yes").
  • Forgetting to increment counters inside the loop: If your correctCount and wrongCount variables never get updated when an answer is judged right/wrong, they’ll stay at 0 forever.
  • Ignoring whitespace in user input: Accidental spaces (like entering " 4 " instead of "4") can cause mismatches—adding trim() to the user’s input fixes this.

Fixed Program Example

Here’s a corrected version that implements all the required features, with comments explaining the fixes:

import java.util.Scanner;

public class QuizProgram {
    public static void main(String[] args) {
        // Sample quiz questions and answers
        String[] questions = {
            "What is the result of 2 + 2?",
            "Is Java an object-oriented programming language?",
            "Which keyword is used to define a class in Java?"
        };
        String[] correctAnswers = {"4", "Yes", "class"};
        
        int correctCount = 0;
        int wrongCount = 0;
        Scanner scanner = new Scanner(System.in);

        // Loop through each question
        for (int i = 0; i < questions.length; i++) {
            System.out.println("Question " + (i + 1) + ": " + questions[i]);
            String userAnswer = scanner.nextLine().trim(); // Trim extra whitespace

            // Check if answer is correct (case-insensitive)
            if (userAnswer.equalsIgnoreCase(correctAnswers[i])) {
                System.out.println("Correct!\n");
                correctCount++; // Increment correct tally
            } else {
                System.out.println("Wrong! The correct answer is: " + correctAnswers[i] + "\n");
                wrongCount++; // Increment wrong tally
            }
        }

        // Display final results
        System.out.println("=== Quiz Complete ===");
        System.out.println("Total questions: " + questions.length);
        System.out.println("Correct answers: " + correctCount);
        System.out.println("Wrong answers: " + wrongCount);
        // Optional: Calculate percentage score
        double scorePercentage = ((double) correctCount / questions.length) * 100;
        System.out.printf("Your score: %.1f%%%n", scorePercentage);

        scanner.close(); // Clean up the scanner resource
    }
}

Key Fixes Explained

  1. String comparison: Swapped == for equalsIgnoreCase() to handle case variations and proper content matching.
  2. Counter updates: Added correctCount++ and wrongCount++ inside the if/else blocks to track results as each question is answered.
  3. Input handling: Used trim() to remove accidental leading/trailing spaces from user input.
  4. User feedback: Added the correct answer when the user gets a question wrong, which improves the quiz’s usability.

If your specific program has a different structure (like using a different data type for answers, or a loop structure), let me know and I can tailor this further!

内容的提问来源于stack exchange,提问作者Kay Gibs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:02:50