Java版Hangman游戏循环无法提前终止问题求助
修复Hangman游戏提前猜对仍继续循环的问题
问题根源
- 内层
for循环强制执行18次:不管用户是否提前猜中单词,代码都会强制让用户完成18次输入,完全忽略了提前胜利的条件。 checking方法逻辑失效:该方法仅检查第一个字符就终止循环,无法正确判断整个单词是否被猜中。do-while循环条件逻辑错误:原条件会导致循环在已猜中单词后仍继续执行。
修复后的主程序代码
import java.util.Scanner; public class hangman{ public static void main (String args[]){ hangmanMethods m = new hangmanMethods(); Scanner sc = new Scanner (System.in); String name; char guess; int option; boolean checkifcorrect; System.out.println("Welcome to hangman! \nWhat is your name?"); name=sc.nextLine(); if (name.equals("diane")) System.out.println("hello diane. happy birthday :)"); else System.out.println("hello " +name); System.out.println("please press 1 to start playing"); option=sc.nextInt(); switch(option){ case 1: m.hangmanwords(); String randomword = m.generateword(); int numletters = m.countletters(randomword); System.out.println("lets play hangman. a random word has been generated and has " +numletters+ " letters. you have 18 guesses. choose a letter to start."); char[] guessedLetters = new char[numletters]; for (int i = 0; i < numletters; i++) { guessedLetters[i] = '_'; } System.out.println(guessedLetters); int maxattempts = 18; int attempts = 0; do{ // 移除原内层for循环,改为单次输入+判断 guess = sc.next().charAt(0); boolean correct = false; for (int j = 0; j < numletters; j++) { if (randomword.charAt(j) == guess) { guessedLetters[j] = guess; correct = true; } } if (correct) { System.out.println("correct guess! well done"); System.out.println(guessedLetters); } else { System.out.println("incorrect guess :(((("); } attempts++; checkifcorrect = m.checking(numletters, randomword, guessedLetters); // 每次输入后检查是否猜中,若猜中直接跳出循环 if(checkifcorrect){ break; } }while (attempts < maxattempts && !checkifcorrect ); // 修正循环条件:有剩余次数且未猜中时继续 if (checkifcorrect) System.out.println("yayyyy you got the word ♥♥♥♥"+" would you like to play again? input 1 to continue or 2 to exit :)"); else System.out.println("you used up all of your guesses :(( the word was " +randomword+". would you like to play again? press 1 to continue or 2 to exit"); option=sc.nextInt(); break; case 2: System.out.println("thanks for playing"); break; } sc.close(); } }
修复后的方法类代码
import java.util.Scanner; public class hangmanMethods{ private String[]words = new String[20]; public void setArray (String[] numbers) { this.words = numbers; // 原代码赋值错误,修正为传入的数组 } public String[] getArray () { return words; } public void hangmanwords(){ //initialising the words words[0]= "sushi"; words[1]= "korea"; words[2]= "computing"; words[3]= "caty"; words[4]= "thomas"; words[5]= "winter"; words[6]= "chocolate"; words[7]= "kimchi"; words[8]= "rain"; words[9]= "plants"; words[10]= "kinnie"; words[11]= "strawberries"; words[12]= "raptures"; words[13]= "pacman"; words[14]= "coldplay"; words[15]= "asos"; words[16]= "shopping"; words[17]= "tkmaxx"; words[18]= "flamingo"; words[19]= "blue"; } public String generateword() { int randomlocation = (int) (Math.random() * 20); // 原代码乘19会漏掉最后一个元素,修正为20 String randomWord = words[randomlocation]; return randomWord; } public int countletters(String word) { return word.length(); } public boolean checking (int numletters, String randomword, char[] guessedLetters) { boolean checkcorrect = true; for( int i = 0; i<numletters; i++){ if (guessedLetters[i] != randomword.charAt(i)){ checkcorrect = false; break; // 发现不匹配时立即终止循环并返回false } } return checkcorrect; } }
额外修复点说明
- 修正
setArray方法的赋值错误:原代码this.words = words是无效赋值,改为this.words = numbers。 - 修正
generateword方法的随机范围:原代码Math.random() *19会导致最后一个元素(索引19)永远不会被选中,改为*20。 - 每次输入后立即检查是否猜中单词,若猜中直接跳出循环。
- 简化最终胜利判断逻辑:只需判断
checkifcorrect即可,无需再判断attempts<maxattempts。
内容的提问来源于stack exchange,提问作者Catriona
相关产品推荐
相关产品推荐

