我正在嘗試在簡單的java中創建一個cee-lo程序。我只是在學習。但是,當我到達我的即時w。 (我簡化了它的測試)它總是返回false。我似乎無法弄清楚爲什麼。它甚至顯示正確的數據,但是當它比較時它失敗了。不正確返回的方法中的布爾值(數組正式參數)
public class ceeLo
{
public static void main (String [] args)
{
Scanner scan= new Scanner (System.in);
int [] die = new int [3];
int answer;
boolean roll = true;
boolean qualifed;
boolean instantW;
boolean instantL;
do
{
System.out.println("Roll the dice?");
answer = scan.nextInt();
if (answer == 0)
roll= false;
else
{
int i;
for (i = 0; i < die.length; i++)
{
die[i]= rollin();
System.out.println(diceTxt(die[i]));
}
qualifed = (qualify (die));
System.out.println("Qualified = " + qualifed);
instantW = (easyW (die));
System.out.println("Instant win = " + instantW);
}
}
while (roll);
}
// Generate random numbers for the roll
public static int rollin()
{
Random rand = new Random();
int die= rand.nextInt(6);
return die;
}
//Check if dice qualify with pair
public static boolean qualify (int [] die)
{
boolean qualify;
//Pair Qualifying roll
if (die[0] == die[1] || die[0] == die[2] || die[1] == die[2])
qualify = true;
else
qualify = false;
return qualify;
}
//Check if instant win
public static boolean easyW (int [] die)
{
boolean instantW;
// show contents of die [x] for testing
System.out.println (die[0] + "" + die[1] + "" + die[2]);
if (die[0] > 2 && die [1] > 2 && die[2] > 2)
instantW = true;
else;
instantW = false;
return instantW;
}
}
首先在最後一個分號之後有一個分號比不應該在這裏,easyW優於2的那三個值? – talnicolas
趕上@talnicolas。始終在代碼塊周圍使用大括號。並在您的IDE中設置代碼以檢測此類問題。注意這個if/else塊可以用'instantW =(die [0]> 2 && die [1]> 2 && die [2]> 2);'替換。 –