Java hangman项目无法正常停止

时间:2017-11-02 15:13:25

标签: java

我正在尝试为我的Uni制作一个Hangman程序,我需要一些帮助。 经过一次尝试,该计划进展顺利。最后你可以选择玩另一个游戏或停止程序,但它保持答案是的,要求换一个新单词。一些尝试后如何访问菜单。你能救我吗?

import java.util.Scanner;

public class MainGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean found = true;
        boolean playAgain = true;
        int life = 8;

        while (playAgain) {
            System.out.println(" MAIN MENU ");
            System.out.println("- Start a new Hangman Game (S) ");
            System.out.println("- Exit (E) ");
            System.out.println("Please enter your choice : ");

            String answer = sc.nextLine().toUpperCase();

            if (answer.equals("E")) {
                playAgain = false;
            } else {
                System.out.println("Give Word");
                String word = sc.nextLine();
                char[] filler = new char[word.length()];
                int i = 0;
                while (i < word.length()) {
                    filler[i] = '-';
                    i++;
                }

                while (found) {
                    System.out.print("The Random Word is now : ");
                    System.out.println(filler);
                    System.out.print("You have " + life);
                    System.out.println(" Lives left");

                    System.out.print("Your Guess : ");
                    char guess = sc.next().charAt(0);
                    guess = Character.toUpperCase(guess);

                    System.out.println(guess);

                    int j = 0;
                    if (word.contains(guess + "")) {
                        System.out.println("The Guess is Correct !!");
                        for (j = 0; j < word.length(); j++) {
                            if (word.charAt(j) == guess) {
                                filler[j] = guess;
                            }
                        }
                    } else {
                        life--;
                    }

                    if (word.equals(String.valueOf(filler))) {
                        System.out.println("Congratulations You Won! Your Guessed Word is : " + word);
                        found = false;
                    }

                    if (life == 0) {
                        found = false;
                        System.out.println("Game Over.");
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

  • 使用大写For Each foundFile As String In My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyDocuments, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, a.MainWindowTitle) msgbox(foundFile) Next
  • 在循环开始之前添加word
  • 替换了sc.next()sc.nextLine()
    有关详细信息,请访问Understanding Scanner's nextLine(), next(), and nextInt() methods

    found = true