0
我遇到了一個隨機錯誤或錯誤的問題。 每次取決於代碼,它只會執行true或false操作。 有沒有人有解決方案,使其實際上是隨機的?爲什麼總是這樣?
int times;
for (left = 0; left < times; left++) {
boolean result = (Math.random() < 0.5);
if(result = true){
System.out.println("Heads");
}
else if(result = false){
System.out.println("Tails");
}
System.out.println(result);
}
您需要使用'如果(結果==真)',或者只是'if(result)'而不是。 'if(result = true)'使'result'爲true。 –
你應該使用==運算符來進行比較。即'result == true' –
你的兩個方法都可以工作!謝謝 – INeedHelp