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 ofequals()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 useequals()(orequalsIgnoreCase()if you want to ignore case, like accepting "yes" instead of "Yes"). - Forgetting to increment counters inside the loop: If your
correctCountandwrongCountvariables 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
- String comparison: Swapped
==forequalsIgnoreCase()to handle case variations and proper content matching. - Counter updates: Added
correctCount++andwrongCount++inside the if/else blocks to track results as each question is answered. - Input handling: Used
trim()to remove accidental leading/trailing spaces from user input. - 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
相关产品推荐
相关产品推荐

