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("/|\\");
}
}
}
答案 0 :(得分:0)
尝试再次检查您的 checkThenumOfGuesses 。您不会显示菜单要求玩家选择1或2。
答案 1 :(得分:0)
据我所知,问题在于您始终留在
while (remainingGuess > 0)
您的checkTheWord
方法中的循环。如果输入的单词错误,则继续循环直到没有尝试为止。
由于您在静态checkTheWord
方法中调用guessTheWord
方法后就中断了,因此程序也总是在此之后终止。您可能应该在使用该方法之后继续操作,而不应该在checkTheWord
方法中循环,因为guessTheWord
中的循环无论如何都具有相同的循环条件。