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

Java中Hangman游戏重复字母检测逻辑失效问题求助

Fixing the Duplicate Guess Check in Your Hangman Game

Hey Ashley, let's break down why your duplicate letter detection isn't working as expected, and walk through some solid fixes.

What's Wrong With the Current Code?

Your repeatedLetters char array approach has two critical issues:

  1. Overwriting All Previous Entries: Every time you guess a new letter, your loop iterates through all 26 positions of the array. For every index where the value doesn't match your guess, you set that index to the guess. This means the entire array gets overwritten with your latest guess every time—any record of previously guessed letters is completely lost.
  2. Unvalidated Re-Guesses: If you do detect a duplicate, you read a new guess but don't re-check if that new guess is also a duplicate. Plus, the loop continues running through the rest of the array after you read the new guess, leading to messy, unplanned behavior.

Solution 1: Use a Boolean Array (Simple, Efficient)

A boolean array is perfect here—each index corresponds to a letter (0 = 'A', 1 = 'B', ..., 25 = 'Z'), and we just mark a position as true when that letter is guessed.

// Boolean array where index 0 = 'A', 1 = 'B', ..., 25 = 'Z'
boolean[] guessedLetters = new boolean[26];
int incorrect = 0;
Scanner kb = new Scanner(System.in); // Assume you've initialized your Scanner

while (incorrect < 7) {
    System.out.println("\nGuess a letter: ");
    char guess = kb.next().toUpperCase().charAt(0);
    
    // Convert the character to its corresponding array index
    int letterIndex = guess - 'A';
    
    // Check if this letter was already guessed
    if (guessedLetters[letterIndex]) {
        System.out.println("You already guessed " + guess + ". Please try another one.");
        continue; // Skip the rest of the loop and let the user guess again
    }
    
    // Mark the letter as guessed
    guessedLetters[letterIndex] = true;
    
    // --- Add your game logic here (check if guess is correct, update incorrect count, etc.) ---
    // ...
}

Solution 2: Use a HashSet (Cleaner, More Flexible)

If you prefer a more readable approach, a HashSet<Character> works great because Sets automatically reject duplicates, and checking for existing values is straightforward.

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

// ...

Set<Character> guessedLetters = new HashSet<>();
int incorrect = 0;
Scanner kb = new Scanner(System.in);

while (incorrect < 7) {
    System.out.println("\nGuess a letter: ");
    char guess = kb.next().toUpperCase().charAt(0);
    
    // Check if the letter is already in the set of guessed letters
    if (guessedLetters.contains(guess)) {
        System.out.println("You already guessed " + guess + ". Please try another one.");
        continue;
    }
    
    // Add the new guess to the set
    guessedLetters.add(guess);
    
    // --- Add your game logic here ---
    // ...
}

Both solutions will properly track which letters have been guessed, and gracefully handle duplicate inputs without losing previous data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:15:38