Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法始终返回True_Java - Fatal编程技术网

Java 方法始终返回True

Java 方法始终返回True,java,Java,出于某种原因,这总是返回true。 知道为什么吗?我已经测试了您的代码,下面的部分似乎工作正常: public static boolean checkGuess(String getGuess, double getBet){ double num = Math.round(Math.random()*10); boolean correctSide = false; if (num <=5 && getGuess.equals("H"))

出于某种原因,这总是返回true。
知道为什么吗?

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

    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;
}
也请分享你在updateBalcorrectSide,getBet中所做的事情

就在这里:

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;

您必须检查:ifcorrectSide==true。你现在做的是correctSide=true,所以你总是说correctSide是true。

这应该做什么?你调试过吗?不可能。Math.roundMath.random始终为1。@ohaleck有时为0。看起来您希望使用RandomnextBoolean以及Coin.HEADS和Coin.TAILS的枚举。Math.roundMath.random并不总是返回1。Math.random返回一个介于0和1之间的值,Math.round有以下实现:public static long round double a{return int floor a+0.5f;}因此该方法没有返回true,而是updateBal始终添加赌注。
 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;
    }