为什么当我猜错了单词时我的菜单没有显示?


package hangman;
import java.util.ArrayList;
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) {
int maxGuess = 5; // chances or life
String wordTobeGuessed = "philippines";
System.out.println("Welcome To Hangman!");
System.out.println("You have 5 chances to guess the word!");
System.out.println();
// method calls
guessTheWord(wordTobeGuessed, maxGuess);
}
/**
* Function to manipulate the string
* @param word           is the secret word
* @param remainingGuess is the number of chances
*/
public static void guessTheWord(String word, int remainingGuess) {
@SuppressWarnings("resource")
Scanner userInput = new Scanner(System.in);
char[] yourWord = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
yourWord[i] = '-';
if (word.charAt(i) == ' ') {
yourWord[i] = ' ';
}
}
System.out.print(yourWord);
// print out the remainingGuess which is 5
System.out.println("  Your chance remaining = " + remainingGuess);
ArrayList<Character> containerForChars = new ArrayList<Character>();
// while the condition is true
while (remainingGuess > 0) {
System.out.println("Press 1 if you want guess the secret word  nPress 2 to guess a 
letter");
int num = userInput.nextInt();
System.out.println();
if (num == 1) {
checkTheWord(word, remainingGuess, yourWord);
break;
}

当程序启动时,程序要求玩家在 1 和 2 之间进行选择,如果您想猜出秘密单词,请选择 1,如果您想猜出一个字母,请选择 2。当我按 2 时,程序运行良好,但是当我按 1 时,程序将要求玩家按预期输入他/她猜到的单词。如果猜出的单词与秘密单词匹配,则工作正常,但问题是,如果玩家键入了错误的猜词,则要求玩家按 1 猜单词或按 2 猜字母的菜单不会显示。它只要求玩家再次猜测秘密单词。

if (num == 2){
System.out.println("Please Type a letter: ");
char typedLetter = userInput.next().charAt(0); // char user input
// check the arrayList to eliminate duplicates
if (containerForChars.contains(typedLetter)) {
System.out.println("You have already tried that letter");
continue;
}
containerForChars.add(typedLetter); 
if (word.contains(typedLetter + "")) {
for (int y = 0; y < word.length(); y++) {
if (word.charAt(y) == typedLetter) {
yourWord[y] = typedLetter; 
}
}
} else {
remainingGuess--; 
checkThenumOfGuesses(remainingGuess, word);
}
if (word.equals(String.valueOf(yourWord))) { 
// prints out
System.out.println(yourWord);
System.out.println("Congratulations you guessed the Word!");
break; // stops the game
}           
if (remainingGuess != 0) {
System.out.print(yourWord);
System.out.println("  tries remaining = " + remainingGuess);
}
}
}
}
public static void checkTheWord(String word, int remainingGuess, char[] yourWord) {
ArrayList<String>data= new ArrayList<String>();

while (remainingGuess > 0) {
System.out.println("Please write your guessed word");
@SuppressWarnings("resource")
Scanner userInput = new Scanner(System.in);
String guessWord = userInput.nextLine();
if (data.contains(guessWord)) {
System.out.println("You have already tried that word");
continue;
}
data.add(guessWord);
if (guessWord.equals(word)) {
System.out.println("Congratulations! You have guessed the secret word!");
break;
} else {
remainingGuess--;
checkThenumOfGuesses(remainingGuess, word);
}
if (remainingGuess != 0) {
System.out.print(yourWord);
System.out.println("  tries remaining = " + remainingGuess);
}
}
}
/**
* This method draw the hangman according to the number of remaining guesses
* @param remainingGuess is the remaining chance of the player
* @param word           is the secret word to be guessed
*/
public static void checkThenumOfGuesses(int remainingGuess, String word) {
if (remainingGuess == 0) {
System.out.println("You Lose! R.I.P." +
"n ________" + 
"n |       |"+ 
"n |       Ö"+ 
"n |      /|\"
+ "n |      / \" + 
"n |       " +
"n/|\     ");
System.out.println();
System.out.println("The secret word is " + word);
}
else if (remainingGuess == 1) {
System.out.println(" ________" 
+ "n |       |" + 
"n |" 
+ "n |" + 
"n |" + 
"n |" + 
"n/|\");
} else if (remainingGuess == 2) {
System.out.println(" ________" + 
"n |" + 
"n |" + 
"n |" + 
"n |" + 
"n |" + 
"n/|\");
} else if (remainingGuess == 3) {
System.out.println(" |" 
+ "n |" 
+ "n |" 
+ "n |" 
+ "n |"
+ "n |" 
+ "n/|\");
} else if (remainingGuess == 4) {
System.out.println("/|\");
}
}
}

据我所知,问题是你总是停留在

while (remainingGuess > 0)

checkTheWord方法中循环。如果输入的单词不正确,请继续循环,直到没有剩余的尝试。 由于在静态guessTheWord方法中调用checkTheWord方法后中断,因此程序也将始终在此之后终止。您可能应该在使用该方法后继续,而不是在checkTheWord方法中循环,因为guessTheWord中的循环无论如何都具有相同的循环条件。

尝试再次检查您的检查ThenumOfGuesses。您不会显示要求玩家选择 1 或 2 的菜单。

最新更新