为什么当我猜错单词时我的菜单没有显示?
Why my menu is not showing when I guess a wrong word?
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 时,程序会要求玩家按预期输入 his/her 猜出的单词。如果猜出的词与秘密词相匹配,它就可以正常工作,但问题是如果玩家输入了错误的猜词,则不会显示要求玩家按 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("/|\");
}
}
}
尝试再次检查您的 checkThenumOfGuesses。您没有显示要求玩家选择 1 或 2 的菜单。
据我所知,问题是你总是停留在
while (remainingGuess > 0)
在您的 checkTheWord
方法中循环。如果单词输入错误,则继续循环,直到没有尝试为止。
由于您在静态 guessTheWord
方法中调用 checkTheWord
方法后中断,程序也将始终在此之后终止。您可能应该在使用该方法后继续,而不是在 checkTheWord
方法内循环,因为 guessTheWord
中的循环无论如何都具有相同的循环条件。
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 时,程序会要求玩家按预期输入 his/her 猜出的单词。如果猜出的词与秘密词相匹配,它就可以正常工作,但问题是如果玩家输入了错误的猜词,则不会显示要求玩家按 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("/|\");
}
}
}
尝试再次检查您的 checkThenumOfGuesses。您没有显示要求玩家选择 1 或 2 的菜单。
据我所知,问题是你总是停留在
while (remainingGuess > 0)
在您的 checkTheWord
方法中循环。如果单词输入错误,则继续循环,直到没有尝试为止。
由于您在静态 guessTheWord
方法中调用 checkTheWord
方法后中断,程序也将始终在此之后终止。您可能应该在使用该方法后继续,而不是在 checkTheWord
方法内循环,因为 guessTheWord
中的循环无论如何都具有相同的循环条件。