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如果你想猜 secret 单词,2如果你想猜字母。当我按 2 时,程序运行得很好,但当我按 1 时,程序会要求玩家按预期输入他/她猜测的单词。如果猜测的单词与 secret 单词匹配,则可以正常工作,但问题是,如果玩家输入了错误的猜测单词,则不会显示要求玩家按 1 猜测单词或按 2 猜测字母的菜单。它只是要求玩家再次猜测 secret 单词。
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
中的循环无论如何都具有相同的循环条件。
关于java - 当我猜错单词时,为什么我的菜单不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210776/