do-while条件不会停止循环Java [英] do-while conditions isn't stopping loop Java

查看:188
本文介绍了do-while条件不会停止循环Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为 ConsoleHangmanGame 的对象,它只包含玩游戏和显示结果的方法。我在 ConsoleHangman 类中实例化了一个 Hangman 对象。 Hangman 对象执行游戏的所有准备和处理。奇怪的是我在 ConsoleHangmanGame 中有 playGame()方法的while循环,但它不是满足条件时停止循环。

so I have an object called ConsoleHangmanGame which just contains the methods to play the game and displaying the results. I instantiated a Hangman object within the ConsoleHangman class. The Hangman object does all the preparation and processing for the game. The weird thing is that I have a do while loop for the playGame() method inside of ConsoleHangmanGame, but it's not stopping the loop when the conditions are met.

这是我的playGame方法

Here's my playGame method

public boolean playGame()
{
    // Declaration
    String letter;
    int result = 0;
    boolean vali;

    // Statement
    try
    {
        hg.prepGame(); // call prepGame method

        do
        {
            displayWord();

            System.out.print("\nPlease enter a letter: ");
            letter = scan.next();
            letter = letter.toLowerCase();
            vali = letter.matches("[a-z]");

            if(vali == true)
            {
                result = hg.process(letter); // call process method
                displayResult(result);
            }
            else
            {
                System.out.println("Sorry, not a valid input.\n");
            }

        }
        while(result != 2 || result != -2); 
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        System.out.println(e);
        return false;
    }

    return true;
}// end playGame method

变量结果是从流程获取返回值来自Hangman类的方法。

The variable result is getting the return value from the process method from the Hangman class.

这是我的处理方法。

public int process(String letter)
{
    // Declaration
    int i;
    int stPos = 0;
    boolean found = false;
    String pS = this.ans;

    // Statement


/******* Some code here *******/

    if(found == false)
    {
        if(lifeLine == 0)
        {
            return gameOver; // no lives left
        }
        else
        {
            lifeLine--;
            return wrong; // wrong guess
        }
    }       
    else if(found == true && this.ans.compareTo(this.rightAns.toString()) == 0)
    {
            return complete; // complete game
    }
    else
    {
        return right; // right answer, but incomplete game
    }

}// end process method

这可能是我返回不使循环停止的值的方式吗?我的老师告诉我们使用public static final int作为回报。所以我在 Hangman 类中声明了它们。

Could it be the way that I'm returning the value that's not making the loop stop? My teacher told us to use public static final int for the returns. So I declared them in the Hangman class.

public static final int right = 1;
public static final int wrong = -1;
public static final int complete = 2;
public static final int gameOver = -2;

任何帮助都会受到赞赏,它试过,调试并看到当我赢得游戏时它确实返回值2,但它不会使我的循环停止。我将继续调试,希望有人可以分享他们的想法。谢谢。

Any help would be appreciated, It tried, debugging and saw that when I did win a game it does return a value of 2, but it's not making my loop stop. I will continue debugging and hopefully someone could share their thoughts. Thanks.

推荐答案

逻辑上,通过 De Morgan定律

result != 2 || result != -2

!(result == 2 && result == -2)

这总是一个真实的表达。

which is always a true expression.

条件应该是

!(result == complete || result == gameOver)

,当应用上述相同的法律时,

which, when applying the same law as above, is

result != complete && result != gameOver

(使用常量 - 虽然我更喜欢像GAMEOVER这样的大写符号 - 而不是魔术数字也使代码更容易阅读。)

(Using the constants - although I would prefer uppercase symbols like GAMEOVER - instead of the magic numbers also makes the code easier to read.)

这篇关于do-while条件不会停止循环Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆