Hangman循环问题

时间:2016-09-28 10:41:09

标签: java

我被要求制作一个刽子手游戏。我遇到了问题。当我问用户他们是否想再玩一次时,用户会输入Y,但游戏不会重新开始正确的方式,因为它不会选择一个新单词而只是继续要求猜出这封信,当这封信输入时停止游戏并询问用户是否要再次玩游戏。有人可以告诉我如何循环我的程序,以便用户可以再次玩游戏。 包刽子手。

public static void main(String args[]) throws IOException {

    Scanner keyboard = new Scanner(System.in);
    int random = (int) (Math.random() * 5);
    String s = null;
    InputStream input = null;
    int play = 0;
    Path file = Paths.get("H:\\Varsity work\\Java                        
         Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt");
        input = Files.newInputStream(file);
        BufferedReader reader = new BufferedReader(new                                                      InputStreamReader(input));
        ArrayList<String> lines = new ArrayList<String>();
        while ((s = reader.readLine()) != null) {
            lines.add(s);
        }
        String[] linesArray = lines.toArray(new String[lines.size()]);
        String[] randomWord = new String[1];
        while (play == 0) {
            System.err.printf("Welcome to hangman.\n");
            randomWord[0] = linesArray[random];
            System.out.println(randomWord[0]);
            Random ran = new Random();
            String word = randomWord[ran.nextInt(randomWord.length)];
            char[] CharArr = word.toCharArray();
            char[] dash = word.toCharArray();
                for (int i = 0; i < dash.length; i++) {
                    dash[i] = '-';
                    System.out.print(dash[i]);
                }
            for (int i = 1; i <= dash.length; i++) {
                System.out.printf("\nGuess a Letter:");
                char userLetter = keyboard.next().charAt(0);

                for (int j = 0; j < CharArr.length; j++) {
                    if (userLetter == dash[j]) {
                        System.out.println("this word already exist");
                    } else if (userLetter == CharArr[j]) {
                        dash[j] = userLetter;
                        i--;
                    }
                }
                System.out.print(dash);
                if (word.equals(new String(dash))) {
                    System.out.println("\nYou have guessed the word correctly!");
                    System.out.println("Play adian? (y/n)");
                    String name = keyboard.next();
                    if(name.equals("y")) {
                        play = 0;
                    } else if(name.equals("n")) {
                        play = 1;
                        return;
                    }
                 }                
            }    
        }
    } 

1 个答案:

答案 0 :(得分:2)

根据您当前的逻辑,这是不可能的,因为您的代码会变得混乱。您必须将代码划分为方法。我会通过以下方式将其划分为方法:

setUpGame();
game();
cleanUpAfterGame();

setUpGame() {
    chooseWord();
    paintBasicHangman();
}

game() {
    while (alive) {
        readLetterFromUser();
        if (missedLetter) {
            paintNextPartOfHangman();
        } else {
            redraw();
            if (won()) {
                return true;
            }
        }
    }
    return false;
}

controller() {
    do {
        setUpGame();
        game();
        cleanUpAfterTheGame(); //optional
        wantsNewGame?();
    } while (userWantsToPlay)