(Java) HangMan 游戏:虽然循环逻辑似乎关闭

背景:我正在通过赫尔辛基大学的大规模开放在线课程 (MOOC) 计划学习 Java。目标之一是构建基于文本的刽子手游戏。这里有更多参考:https : //materiaalit.github.io/2013-oo-programming/part1/week-2/


我想出了大部分代码。但是,游戏执行起来很尴尬。为了扩展,游戏应该一直运行,直到猜出的次数或计数达到 7。因此,while 循环会一直工作,直到达到 7 或猜对单词。


问题:我猜的话是丰田。printWord() 方法通过这样做来隐藏单词:******。每次用户猜测字母时,它都会删除星号。所以,如果我猜出正确的字母,它应该这样做:*o**o。但是,当用户输入下一个字母时,它会忘记猜到了“o”。所以,它目前是这样工作的:


User: o

printWord(): *o*o**

User: T

printWord(): T*****

这是我的主要课程:


public class main {


    public static void main(String[] args) {


        String word = "Toyota";

        int count = 0; 


         Hangman hangman = new Hangman();


            System.out.println("************");

            System.out.println("* Hangman *");

            System.out.println("************");

            System.out.println("");

            hangman.printMenu();

            System.out.println("");


            Scanner reader = new Scanner (System.in);

            String input = reader.nextLine();


            if (input.equals("quits")){


        System.out.println("Thank you for playing!");

            }else{

        while (count < 7){      

            char c = reader.next().charAt(0);

            hangman.printWord(c);

            hangman.printMan(count);

            hangman.printStatus(c);

            count++; 


        }

            }



}

}

我假设将方法 printWord() 设为静态是问题所在,因为创建该方法的新实例会重置所有内容。但是,删除该关键字不起作用。出于某种原因,printStatus 可以跟踪使用的字母,但不能跟踪实际计数。



DIEA
浏览 142回答 1
1回答

慕工程0101907

创建单词和空白全局变量以及在调用构造函数时设置变量的构造函数。像下面。将创建空白字符数组的行printWord()移到构造函数中。因为每次调用时printWord都会创建一个新的空白数组或替换旧数组,这就是为什么打印空白是不正确的。您只想设置一次空白字符数组,构造Hangman()将是最好的地方,因为在创建 Hangman 对象 ( Hangman hangman = new Hangman();)时只调用一次。我希望这是有道理的。让我知道这是否解决了您的问题。添加/更新代码public class Hangman {&nbsp; &nbsp; private String word;&nbsp; &nbsp; private char[] blank;&nbsp; &nbsp; private int guesses;&nbsp; &nbsp; public Hangman() {&nbsp; &nbsp; &nbsp; &nbsp; word = "Toyota";&nbsp; &nbsp; &nbsp; &nbsp; blank = new char [word.length()];&nbsp; &nbsp; &nbsp; &nbsp; for (int i =0; i < word.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blank[i] = '*';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; guesses = 0;&nbsp; &nbsp; }&nbsp; &nbsp; // Optional Constructor - Hangman hangman = new Hangman("Toyota");&nbsp; &nbsp; public Hangman(String word) {&nbsp; &nbsp; &nbsp; &nbsp; this.word = word;&nbsp; &nbsp; &nbsp; &nbsp; blank = new char [word.length()];&nbsp; &nbsp; &nbsp; &nbsp; for (int i =0; i < word.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blank[i] = '*';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; guesses = 0;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; * Rest of your code&nbsp; &nbsp; */}删除代码- 删除从printWord()方法中标记的注释行&nbsp; &nbsp; //String word = "Toyota";&nbsp;&nbsp; &nbsp; int count = 0;&nbsp;&nbsp; &nbsp; //char [] blank = new char [word.length()];&nbsp; &nbsp; //for (int i =0; i < word.length(); i++){&nbsp; &nbsp; //&nbsp; &nbsp; blank[i] = '*';&nbsp; &nbsp; //}&nbsp;更新 - 更改 printStatuspublic static void printStatus(char c){&nbsp; &nbsp; String mystring = "abcdefghijklmnopqrstuvwxyz";&nbsp; &nbsp; int count = 0;&nbsp;&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; sb.append(mystring);&nbsp; &nbsp; for (int i = 0; i < sb.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if (c ==sb.charAt(i)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.deleteCharAt(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; guesses++;&nbsp; &nbsp; System.out.println(sb.toString());&nbsp; &nbsp; System.out.println("The number of guesses are: " + guesses);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java