Hangman检查使用过的猜测形成用户

时间:2016-10-23 16:05:48

标签: java arrays

我正在寻找一些帮助扫描数组中的用户输入中使用过的字母并告诉他们,所以我在这一点上迷失了。

/**
     * 
     */
    package Hangman;

    import java.io.IOException;

    /**
     * @author
     *
     */
    public class Hangman {
        static int choice;
        static String solve;
        static String promt;
        static String guess;
        static char chaGuess;
        static char[] guesses = new char[25];
        static char[] check;
        static int counter = 1;
        static boolean contine;

        public static void main(String[] args) throws IOException,       InterruptedException {
            playerOne();
            for (int i = 0; i < 300; i++) {
                System.out.println("  ");
            }
            while (contine = true) {
                playerTwo();
                choiceMade(choice);
            }
        }

        public static String playerOne() throws IOException {
            System.out.println("Welcome player one");
            promt = ConsoleUI.promptForInput("Enter a Phrase or word for Player Two to guess", true);
            return promt;
        }

        public static int playerTwo() throws IOException {
            String[] ask = new String[3];
            ask[0] = "Give up";
            ask[1] = "Guess a letter";
            ask[2] = "Solve the puzle";
            choice = ConsoleUI.promptForMenuSelection(ask, true);
            return choice;
        }

        public static void choiceMade(int c) throws IOException {
            if (c == 0) {
                contine = false;
                System.out.println("Thank you for playing");
                System.exit(0);
            }
            if (c == 1) {
                guess = ConsoleUI.promptForInput("Enter your guess", true);
                guessChar(guess);


                contine = true;
            } else if (c == 2) {
                solve = ConsoleUI.promptForInput("Enter your answer", true);

                if (solve.equals(promt)) {
                    System.out.println("You solved it you're amazing");
                    contine= false;
                } else {
                    System.out.println("Sorry you guessed it wrong.");
                    System.exit(0);
                    contine= false;
                }
            }
        }

        public static char guessChar(String g) {
            chaGuess = g.charAt(0);
            promt = promt.replace(" ", "");
            check = promt.toCharArray();

                    if(guesses[counter -1] != chaGuess)
if (chaGuess >= 'a' && chaGuess <= 'z' || chaGuess >= 'A' && chaGuess <= 'Z') {
                            guesses[counter] = chaGuess;


                        System.out.println(guesses);
                    }
                    if(guesses[counter - 1] == chaGuess)
                    {
                        System.out.println("You all ready guessed that");
                    }
                }
                counter++;
            return chaGuess;
        }
    }

1 个答案:

答案 0 :(得分:-1)

使用字符串,将每个用过的猜测附加到它的末尾,然后只使用indexOf()