简体   繁体   中英

How do I return something as true or false when I have an if() statement?

I don't exactly know how to word this, but I want to know how to return whether something is true or not depending on what the if statement below determines. I'm sorry if you can't understand what I'm asking, but if you look at the code I think you will understand what I'm asking.

Code

public boolean isFinished()
    {
        int row = 0;
        int column = 0;
    if(box[row][column].getValue() != 0)
            {
                if(box[row][column].getValue == box[row][column+1].getValue() && box[row][column].getValue == box[row][column+2].getValue())
                {
                    return true;
                }
                else if(box[row+1][column].getValue == box[row+1][column+1].getValue() && box[row+1][column].getValue == box[row+1][column+2].getValue())
                {
                    return true;
                }
                 else if(box[row+2][column].getValue == box[row+2][column+1].getValue() && box[row+2][column].getValue == box[row+2][column+1].getValue())
                {
                    return true;
                }
                 else if(box[row][column].getValue == box[row+1][column].getValue() && box[row][column].getValue == box[row+2][column].getValue())
                {
                    return true;
                }
                 else if(box[row][column+1].getValue == box[row+1][column+1].getValue() && box[row][column+1].getValue == box[row+2][column+1].getValue())
                {
                    return true;
                }
                 else if(box[row][column+2].getValue == box[row+1][column+2].getValue() && box[row+1][column].getValue == box[row+2][column+2].getValue())
                {
                    return true;
                }
                else
                 {
                     return false;
                 }
            }
            //return whether it's true or false
        }

You are already returning true for box[row][column].getValue() != 0 . In any of those branches, once return is hit, the JVM returns from that method , not just the if-statement.

Now just add an else block to that outer if-then block, that returns something meaningful for your logic.

只需在最终的if语句中测试您的getValue()方法,然后如果您要获取的值就是return true

boolean functions always want something at the end just take the else out and leave return false at the very end it will work. If it get to a return true statement it will pop out of the method before it even hits the false statement at the end.

Test it using an if statement,

if(isFinished){
    //returned true
    //blah blah do something.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM