繁体   English   中英

如何使用从方法(Java)返回的布尔值?

[英]How to use the returned boolean value from method (Java)?

我在使用布尔方法的返回值时遇到麻烦。

我正在测试的分数首先是有效的:

 public boolean isValidScore() {

    boolean isScoreValid = true;

    scoreHit = Integer.parseInt(scored.getText().toString());//convert the 3 dart score to store it as an integer

    int[] invalidScores = {179, 178, 176, 175, 173, 172, 169, 168, 166, 165, 163, 162};

    boolean invalidNumHit = false;
    for (int i = 0; i < invalidScores.length; i++) {
        if (scoreHit == invalidScores[i]) {
            invalidNumHit = true;
        }
    }

    if (scoreHit > 180 || scoreHit > scoreLeft[player] || scoreHit + 1 == scoreLeft[player] || (scoreHit == 159 && scoreLeft[player] == 159) || invalidNumHit) {

        //won't adjust score left if invalid score/checkout entered
        Toast toast = Toast.makeText(getApplicationContext(),
                "You've entered an invalid score, Try again!", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 75);
        toast.show();

        scored.setText("0");//have to reset score to zero as won't do below as will return from method if invalid score

        isScoreValid = false;//will exit method i.e. won't adjust scores and stats and switch player if invalid score entered.
    }

    return isScoreValid;
}

然后,我进入一个调用此方法的方法-如果值是false,我想从此enterClicked()方法退出-这是我的编码方式:

public void enterClicked(View sender) {

    isValidScore();

    if (isValidScore()) {
    } else {
        return;//exit method if invalid score entered.
    }
       //is more code here.....
}

看来isValidScore()的值始终为true-即使我输入了无效的分数(我用吐司消息对其进行了测试)。

您将两次调用isValidScore() ,而忽略第一次调用的结果。 您应该将其删除。

public void enterClicked(View sender) {

    isValidScore(); // remove this line

    if (isValidScore()) {
        ...
    } else {
        return;
    }

}

你可以尝试一下:

    if (scoreHit > 180 || scoreHit > scoreLeft[player] || scoreHit + 1 == scoreLeft[player] || (scoreHit == 159 && scoreLeft[player] == 159) || invalidNumHit) {

        //won't adjust score left if invalid score/checkout entered
        Toast toast = Toast.makeText(getApplicationContext(),
            "You've entered an invalid score, Try again!", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 75);
        toast.show();

        scored.setText("0");//have to reset score to zero as won't do below as will return from method if invalid score

        return false;//will exit method i.e. won't adjust scores and stats and switch player if invalid score entered.
    }
return true;

对于您在上面提出的问题,以下是我的建议。

public void enterClicked(View sender) {

        boolean validScore = isValidScore();

        if (validScore == true) {
             //Do Something
        } else {
            return;//exit method if invalid score entered.
        }
          //Default Code to be excuted
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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