Hangman游戏字母判断错误求助(ErrorHandling方法故障)
Hangman游戏字母判断错误修复方案
问题根源
当前ErrorHandling()方法存在两处致命问题:
- 提示输出逻辑混乱:当输入字母不在目标单词中时,循环遍历已猜出单词后会无条件执行“不在单词里”的打印;若字母已存在,循环内会多次打印“已存在”提示,之后仍会执行“不在单词里”的打印,导致矛盾输出。
- 错误次数统计失效:方法内定义的
miss是局部变量,修改后无法同步到主方法的miss变量,错误次数始终为0。
修复步骤
- 重构
ErrorHandling()方法,使用标志位区分「字母已存在」「字母不存在」两种场景,避免重复输出。 - 让方法返回错误次数的增量(0或1),用于更新主方法中的错误计数。
- 优化判断逻辑:先检查字母是否已被猜出,再判断是否存在于目标单词中,减少无效遍历。
修改后的完整代码
package besingaPi_LE_3_2; import java.util.Scanner; public class GameHangman { public static void main(String[] args) { Scanner input = new Scanner(System.in); int miss = 0; String choice1; String temp; String[] words = {"giraffe", "hippopotamus", "gorilla", "peacock", "armadillo", "rhinoceros", "alligator", "tamaraw", "chinchilla", "salamander"}; int choice; int choice2; mainMenu(); choice = input.nextInt(); input.nextLine(); System.out.println(); do { if(choice == 1){ String wordRandom = Randomizer(words); String mute = secretWord(wordRandom); temp = mute; System.out.println(wordRandom); System.out.println("Note: use only small character/s\nHint: Animals"); while(!mute.equals(wordRandom)) { System.out.print("(Guess) Enter a letter in word " + mute + " > "); choice1 = input.nextLine(); mute = CharacterSearch(wordRandom, mute, choice1); // 接收方法返回的错误次数增量,更新主方法的miss miss += ErrorHandling(wordRandom, temp, mute, choice1); temp = mute; } System.out.println("The word is " + wordRandom + "." + " You missed " + miss + " time" + (miss != 1 ? "s" : "") + "."); System.out.println("\nDo you want to guess another word? Enter [1]Yes or [0]No"); System.out.print("Please input your choice: "); choice2 = input.nextInt(); input.nextLine(); System.out.println(); if(choice2 == 1) { // 重置错误次数 miss = 0; continue; } else if(choice2 == 0) { System.out.println("Thank you!!"); return; } } else if (choice == 0) { System.out.println("Thank you!!"); return; } }while(choice != 0); } public static void mainMenu() { System.out.println("Welcome to Hangman"); System.out.println("[1]START\n[0]EXIT"); System.out.print("Please input your choice: "); } // 修改后的ErrorHandling方法,返回错误次数增量 public static int ErrorHandling(String targetWord, String oldMuted, String newMuted, String choice1) { char guess = choice1.charAt(0); // 先判断字母是否已经在已猜出的单词中 if(oldMuted.indexOf(guess) != -1) { System.out.println(choice1 + " is already in the word."); return 0; } // 再判断字母是否不在目标单词中 if(targetWord.indexOf(guess) == -1) { System.out.println(choice1 + " is not in the word."); return 1; } // 字母正确且未被猜出,无错误提示,返回0 return 0; } public static String CharacterSearch(String word, String muted, String key) { StringBuilder update = new StringBuilder(muted); for(int i = 0; i < word.length(); i++) { if(word.charAt(i) == key.charAt(0)) { update.setCharAt(i,key.charAt(0)); } } return update.toString(); } public static String secretWord(String word) { StringBuilder temp = new StringBuilder(); for(int i = 0; i < word.length(); i++) { temp.append("*"); } return temp.toString(); } public static String Randomizer(String[] words) { int rand = (int)(Math.random() * words.length); return words[rand]; } }
关键修改说明
- ErrorHandling方法:新增
targetWord参数用于判断字母是否存在,使用indexOf()替代循环遍历,逻辑更简洁;通过返回值传递错误次数增量,解决局部变量无法更新主方法计数的问题。 - 主方法:接收
ErrorHandling的返回值更新miss,并在重新开始游戏时重置miss值;优化了最终提示的语法(错误次数为复数时添加s)。 - secretWord方法:改用
StringBuilder提升字符串拼接效率。
内容的提问来源于stack exchange,提问作者Piolo Pascual Besinga
相关产品推荐
相关产品推荐

