方法总是返回True

时间:2013-10-09 21:45:09

标签: java

    public static boolean checkGuess(String getGuess, double getBet){
    double num = Math.round(Math.random()*10);
    boolean correctSide = false;
    if (num <=5 && getGuess.equals("H")){
        correctSide = true;
    } else if (num >=6 && getGuess.equals("T")){
        correctSide = true;
    } else {
        correctSide = false;
    }
    updateBal(correctSide, getBet);
    return correctSide;
}

出于某种原因,这总是返回true。 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

它是:

 public static double updateBal(boolean correctSide, double getBet){
            double balance = getBal();
            if (correctSide = true){
                    balance = getBet * 2 + balance;
                    System.out.println("Correct. Your balance is now $" + balance);
            } else {
                    balance = balance - getBet;
                    System.out.println("Incorrect. Your balance is now $" + balance);
            }
            return balance;
    }

你必须检查:if(correctSide == true)。你现在做的是correctSide = true,所以你总是说rightSide是真的。

答案 1 :(得分:1)

我已经测试了您的代码,以下部分似乎正常工作:

double num = Math.round(Math.random());
boolean correctSide = false;
if (num == 0 && getGuess.equals("H")){
    correctSide = true;
} else if (num == 0 && getGuess.equals("T")){
    correctSide = true;
} else {
    correctSide = false;
}
return correctSide;

请分享您在updateBal中所做的事情(correctSide,getBet);