当前位置: 首页 > 知识库问答 >
问题:

Java hangman项目无法正常停止

华懿轩
2023-03-14

我正试图为我的大学做一个刽子手程序,我需要一些帮助。一次尝试后的程序进展顺利。最后,你可以选择玩另一个游戏或停止程序,但它保持答案是肯定的,要求一个新词。我怎么能访问菜单后,一些尝试。你能帮帮我吗?

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个答案

缑兴贤
2023-03-14

>

  • 用大写.
  • 在循环开始之前添加了找到=true
  • 替换sc.next()sc.nextLine()
    有关详细信息,请访问了解扫描仪的nextLine(),下一步()和nextInt()方法

            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();
                            word = word.toUpperCase();
                            char[] filler=new char[word.length()];
                            int i =0;
                            while (i<word.length()) {
                                filler[i]='-';
                                i++;
                            }
                            found = true;
                            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.nextLine().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.");}
                            }               
                        }                   
                    }           
                }
            }
    

  •  类似资料:
    • 问题内容: 我正在尝试使AspectJ编织工作在一个简单的Maven项目中,并且不确定它出了什么问题:当我使用“ mvn exec:java”运行代码时,看不到预期的输出。 我确定代码可以正常工作,因为我在STS中尝试了相同的方法,在这里工作良好。我只是想让AspectJ在Maven项目中工作。 任何有关如何调试此类问题的提示将不胜感激。 外观文件与代码位于同一文件夹中: Java文件: 问题答案

    • 我几乎完成了一个基本的刽子手游戏,但我在课堂上遇到了“makeGuess”方法的困难。我试图测试用户输入(他们的一个字符猜测)与秘密单词,并更新伪装的单词,如果他们的猜测是正确的或不正确的。任何正确方向的建议或指点都将不胜感激。每次运行时都会出现以下错误: 这是刽子手课: } 这是我的演示课 }

    • 我想在Project Retor中尝试。根据描述,它用于 订阅此单声道并无限期地阻止,直到收到下一个信号或单声道完成空 我尝试了以下方法。 我希望看到“外链”在30年代延迟之前打印出来。然而,我必须等待30多岁。 没有收到信号吗?当收到信号时,程序块应该停止?这是发送信号的正确方式吗?

    • 问题内容: 我使用的字体库字体很棒。当项目没有用grunt构建/丑化时,它可以工作。 但是,当我用粗鲁的方式构建项目时,它不起作用。我在控制台中收到此错误:… / fonts / fontawesome-webfont.woff?v = 4.0.3 404(未找到) 我已经将这个项目与约曼搭在一起。 这是我在index.html中的引用 有什么想法可能是错误的吗? 更新 我需要将文件夹/ bowe

    • 问题内容: 我已经设定 我已经禁用了log4j.properties中的所有消息 但是Hibernate将所有查询和语句写入控制台。 问题答案: 设置为告诉hibernate将 所有SQL语句写入控制台。 这是将日志类别设置为调试的替代方法。 因此,即使将此属性设置为,也请确保没有定义以下类别(或配置为使用控制台附加程序): 另外,请确保在实例化对象时不要将程序设置为true 。寻找这样的事情:

    • 所以我有一个带有日期时间值的输入(从DB获取值),我使用jQuery日期时间选择器来更改日期/时间。 这是我的输入 这是我的Datetimepicker配置: 使用此配置,Datetimepicker将始终从现在开始的日期时间作为默认值。如果我删除选项,我可以在输入中看到数据库中的实际日期/时间。 信息:我在数据库中的日期不是weekend,与现在一样新()。 所以我的问题是,我怎么能使用Date