java - 当我猜错单词时,为什么我的菜单不显示?

标签 java arrays methods menu

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/

相关文章:

c++ - 词频 strcmp 使用结构数组无限工作

Java 数组!不!越界

node.js - 如何调用路由模块(node.js)中的内部函数?

java - 尝试根据用户输入的给定行创建二维数组(java)

C# 方法解析,long 与 int

java - 为什么对于某些值会有无穷大的结果,而对于其他值却没有无穷大的结果?

java - com.microsoft.sqlserver.jdbc.SQLServerException : Incorrect syntax near 'GO'

java - 在 Intellij/Android Studio 中调试 Gradle 构建文件

java - 奇怪的安卓异常

php - Mysql查询mysql_fetch_array