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 = "pie";
System.out.println("Welcome To Hangman!");
System.out.println("You have 5 chances to guess the word!");
System.out.println();
guessTheWord(wordTobeGuessed, maxGuess);
}
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);
System.out.println(" Your chance remaining = " + remainingGuess);
System.out.println("Press 1 if you want to the secret word \nPress 2 to guess a letter");
int num = userInput.nextInt();
System.out.println();
我正在尝试检查猜出的单词是否与要猜出的单词相同,但是此代码是不工作。当我在菜单中选择1时,它会显示println“请输入您的猜词”但它不允许我输入任何内容。我想知道我做错了什么。
if(num == 1) {
System.out.println("Please write your guessed word");
String guessWord = userInput.nextLine();
checkTheWord(guessWord, word);
}
当我在菜单中选择2时,这部分也有问题。它正在工作,但仅如果我想猜要猜的单词,可以输入字符而不是单词。我想要只要我的机会不等于零,就可以显示菜单。
if(num == 2) {
ArrayList<Character> containerForChars = new ArrayList<Character>();
while (remainingGuess > 0) {
System.out.println("Please type a letter! ");
char typedLetter = userInput.next().charAt(0);
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))) {
System.out.println(yourWord);
System.out.println("Congratulations you guessed the Word!");
break;
}
if (remainingGuess != 0) {
System.out.print(yourWord);
System.out.println(" tries remaining = " + remainingGuess);
}
}
}
}
public static void checkTheWord(String yourGuessedWord, String word) {
if(yourGuessedWord.equals(word)) {
System.out.println("Congratulations! You have guessed the secret word!");
}
}
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("/|\\");
}
}
}
我正在尝试检查猜出的单词是否与要输入的单词相同猜到了,但是此代码无法正常工作。当我在菜单中选择1时显示println“请写出您的猜词”,但没有请允许我输入任何内容。我想知道我做错了。
执行以下操作以消除此问题:
System.out.println("Please write your guessed word");
userInput = new Scanner(System.in);
String guessWord = userInput.nextLine();
当我在菜单中选择2时,这部分也有问题。它的可以,但是如果我输入,我只能输入一个字符而不是单词想猜出要猜的词。
由于以下行而发生:
char typedLetter = userInput.next().charAt(0);
由于charAt(0)
,此行只能接受输入的一个字符(第一个字符)。如果您想让它接受一个单词,则需要将其写为
String str = userInput.next();