首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:绞刑者游戏,10次机会,但只有5条命

Java:绞刑者游戏,10次机会,但只有5条命
EN

Stack Overflow用户
提问于 2016-09-29 20:08:10
回答 2查看 144关注 0票数 0

我正在创建一个迷你的Hangman游戏。用户有10次猜测的机会,但只有5次生命。

这个应用程序可以工作,但在第五次生命之后还会继续,尽管我希望它能把玩家踢出那个循环。

可实例化的类(Hangman.java)可以正常工作。

在instantiable类中描述的秘密单词是"julie“。

我的App类:

代码语言:javascript
复制
    import javax.swing.JOptionPane;

    public class HangmanApp {

    public static void main(String args[]) {
    String input, secret, result, playAgain;
    char guess;
    int i, j, k, lives;

    Hangman myHangman = new Hangman();

    do{
        JOptionPane.showMessageDialog(null, "Hello, welcome to Hangman! You have 10 chances but only 5 lives! Best of luck");

        lives = 5;

        for (j = 10; j > 0; j--) {

            while (lives >= 0){
                input = JOptionPane.showInputDialog(null, "Please enter a letter");
                guess = input.charAt(0);

                //process
                myHangman.setGuess(guess);
                myHangman.compute();
                result = myHangman.getResult();

                if ((input.charAt(0) == 'j') || (input.charAt(0) == 'u') || (input.charAt(0) == 'l') || (input.charAt(0) == 'i') || (input.charAt(0) == 'e')) {
                    JOptionPane.showMessageDialog(null, "That letter is in the word! Current correct letters:  " + result + ".");
                } else {
                    lives--;
                    JOptionPane.showMessageDialog(null, "Sorry, that letter is not there. Current correct letters:  " + result + ".");
                }

                //output
                //JOptionPane.showMessageDialog(null, "Current correct letters:  " + result);

            };
            lives = -1;
        }
        result = myHangman.getResult();
        secret = myHangman.getSecret();

        if (secret.equals(result)) {
            JOptionPane.showMessageDialog(null, "Congratulations, you got it!! The word was " + secret + ".");
        } else {
            JOptionPane.showMessageDialog(null, "Sorry, you didn't get it, better look next time! The word was " + secret + ".");
        }

        playAgain = JOptionPane.showInputDialog("Do you want to play again? yes/no");

    }while (playAgain.equals("yes"));
}

}

EN

回答 2

Stack Overflow用户

发布于 2016-09-29 20:11:36

尝试以下更改:

代码语言:javascript
复制
while (lives > 0){

你从5开始,然后下降到4,3,2,1和0。更改后,这将在0处停止

票数 3
EN

Stack Overflow用户

发布于 2016-09-29 20:19:01

代码语言:javascript
复制
    // don't need two nested cycles, you can do it in a single one
    // The cycle exits if any one of the conditions fail
    //  max attempts exhausted or all the lives are lost
    // -------------------v        v------------------------             
    for (j = 10, lives=5; j > 0 && lives > 0 ; j--) {
       // -------------------------------------^
       // j - the number of attempt is decremented for each trial,
       // no matter if successful or not

       //... the rest of cycle logic, which will decrement the lives
       //  only in cases of unsuccessful attempts

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39769909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档