簡體   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