我有一个子手游戏,它运行得很好,除了我无法弄清楚如何让用户猜对整个单词,而不是每次都只猜一个字母。我该怎么办?
public class HangmanHelper {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
String play; // 'y' or 'n'
int missed = 0;
do {
// Generate a random word
char[] word = getWord();
// Display each letter in the word as an asterisk.
char[] asterisks = new char[word.length];
fillAsterisks(asterisks);
do {
// Prompt the user to guess one letter
char guess = getGuess(asterisks);
// Check if is letter is correct
if (!isCorrectGuess(word, asterisks, guess)) {
missed++;
if (missed == 6) {
System.out.printf("Sorry, you've exceeded the max number of incorrect guesses!%n");
System.exit(0);
}
}
} while (!isWordFinish(asterisks));
// Print results
print(word, missed);
// Ask the user whether to continue to play with another word
System.out.println("Do you want to guess another word? Enter y or n>");
play = input.next();
} while (play.charAt(0) == 'y');
}
/* getWord randomly generates a word from a file */
public static char[] getWord() throws FileNotFoundException {
// Create File object
File file = openFile();
// Create an ArrayList
ArrayList<String> words = new ArrayList<>();
try (
// Create input file
Scanner input = new Scanner(file);
){
while (input.hasNext()) {
words.add(input.next());
}
}
// Pick a random string
String pick = words.get((int)(Math.random() * words.size()));
// Convert string to character array
char[] word = pick.toCharArray();
return word;
}
/** Return File object */
public static File openFile() throws FileNotFoundException {
File file = new File("hangman.txt");
// Check if file exists
if (!file.exists()) {
System.out.print("File " + file.getName() + " does not exist");
System.exit(1);
}
return file;
}
/** fillAsterisks initializes a list with asterisks */
public static void fillAsterisks(char[] list) {
for (int i = 0; i < list.length; i++) {
list[i] = '*';
}
}
/** checkGuess tests if the users guess was correct */
public static boolean isCorrectGuess(char[] word, char[] blanks, char guess) {
boolean correct = false;
int message = 2;
for (int i = 0; i < word.length; i++) {
if (word[i] == guess) {
correct = true;
if (blanks[i] == guess)
message = 1;
else {
blanks[i] = guess; // the actual letter is then displayed.
message = 0;
}
}
}
if (message > 0)
print(message, guess);
return correct;
}
/** isWordFinish */
public static boolean isWordFinish(char[] blanks) {
for (char e: blanks) {
if (e == '*')
return false;
}
return true;
}
/** print overloaded */
public static void print(char[] word, int missed) {
System.out.print("The word is ");
System.out.print(word);
System.out.println(" You missed " + missed +
(missed > 1 ? " times" : " time"));
}
/** print overloaded */
public static void print(int m, char guess) {
System.out.print("\t" + guess);
switch (m) {
case 1 : System.out.println(" is already in the word"); break;
case 2 : System.out.println(" is not in the word");
}
}
/** getGuess prompts the user to guess one letter at a time */
public static char getGuess(char[] asterisks){
Scanner input = new Scanner(System.in);
System.out.print("(Guess) Enter a letter in word ");
System.out.print(asterisks);
System.out.print(" > ");
String g = input.next();
return g.charAt(0);
}
}
答案 0 :(得分:3)
您可以选择一个与任何可能的解决方案都不相关的任意短语或输入。例如,在您的getGuess()方法中,您可以执行以下操作:
public static char getGuess(char[] asterisks){
Scanner input = new Scanner(System.in);
System.out.print("(Guess) Enter a letter in word ");
System.out.print(asterisks);
System.out.print(" > ");
String g = input.next();
if (g.equals("I want to guess!"){
// handle the guess here
} else {
return g.charAt(0);
}
}
实施这种解决方案确实有无数种方法,这就是其中一种。
答案 1 :(得分:0)
您可以尝试一下 其中:Word是一个字符串,是输入 “ HangmanWord”是输入所需的字符串
.babelrc
这可能是一个不同的方法,它会先检查该方法,然后在我的方法返回false时使用else语句。