-5
    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。任何想法为什么?

4

2 回答 2

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,所以您总是说correctSide 是真的。

于 2013-10-09T22:01:23.537 回答
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); 中所做的事情;

于 2013-10-09T21:57:09.550 回答