2014-10-11 89 views
1
int Comproll1= (int) (Math.random()*6+1); 
    int Comproll2= (int) (Math.random()*6+1); 
     while (m==1) 
    { 
     { 
     if (Comproll1==1 || Comproll2==1) 
     { 
      System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!"); 
      cr= cr-cr; 
      m++; 
     } 
     else if (Comproll1==1 && Comproll2==1) 
     { 
      System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!"); 
      cp=cp-cp; 
      m++; 
     } 
     else 
     { 
      cr= Comproll1+Comproll2; 
      cp= cp+cr; 
     } 
    } 

嘿大家好!以上是我的代碼 - 無論如何,無論如何,無論如何,總是會顯示第一個選項,即「計算機的一個擲骰子是1,它在一輪中失去了所有點...」。即使我改變陳述的順序,它仍然會這樣做。有人可以向我解釋爲什麼會發生這種情況嗎?謝謝!Math.random if語句錯誤

+0

首先,'int Comproll1 =(int)(Math.random()* 6 + 1)'應該是5 + 1。如果隨機返回6呢?然後你將有一個7的骰子。 – 2014-10-11 23:22:47

+2

@BoratSagdiyev'Math。random()'返回一個浮點數'<1',所以'Math.random()* 6 + 1'的返回值永遠不會高於'6.99 ...',因此如果轉換爲整數,則返回6。 – lexith 2014-10-11 23:25:08

+2

@BoratSagdiyev事實並非如此。 'Math.random()'的結果保證<1,因此,'Math.random()* 6'將始終爲<6,並且'Math.random()* 6 + 1'將始終爲<7 。對'int'的轉換使得6成爲最大值。 – ApproachingDarknessFish 2014-10-11 23:25:29

回答

3

據我所知,因爲你不是再軋

int Comproll1= (int) (Math.random()*6+1); 
int Comproll2= (int) (Math.random()*6+1); 
while (m==1) 
{ 

應該

while (m==1) 
{ 
    int Comproll1= (int) (Math.random()*6+1); 
    int Comproll2= (int) (Math.random()*6+1); 

而且,Java的命名慣例是變量駱駝(和一個開始小寫字母)。所以,Comproll1可能是compRoll1。最後,我個人更喜歡Random.nextInt()和6面骰,可能看起來像

Random rand = new Random(); 
while (m==1) 
{ 
    int compRoll1 = rand.nextInt(6) + 1; 
    int compRoll2 = rand.nextInt(6) + 1; 

編輯其實,你還需要扭轉你的測試順序。因爲如果兩者都是真的,那麼就不可能輸入兩者都是真實的測試。

if (Comproll1==1 || Comproll2==1) { 
    // Here. 
}else if (Comproll1==1 && Comproll2==1) { 
    // Will never enter here. 
} 

切換順序,

if (Comproll1==1 && Comproll2==1) { 
    // Both. 
}else if (Comproll1==1 || Comproll2==1) { 
    // Either. 
} 
+1

是不是因爲他實際上是在第一個if語句中對兩個條件進行ORing?看第二個條件,他是ANDing,所以假設這是真的,第一個if語句也應該是真的。編輯:這可能是兩個原因。 – adchilds 2014-10-11 23:24:16

0

試着改變你的if語句的順序。邏輯上,如果兩個比較中的一個是真的,則第一個語句將執行。在第二條件else if (Comproll1==1 && Comproll2==1)爲真的情況下,第一條件if (Comproll1==1 || Comproll2==1)也將成立。

由於您已經以if-else-if方式鏈接了if語句,因此將執行第一個if語句以等於true。

if (Comproll1==1 && Comproll2==1) 
    { 
     System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!"); 
     cp=cp-cp; 
     m++; 
    } 
    else if (Comproll1==1 || Comproll2==1) 
    { 
     System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!"); 
     cr= cr-cr; 
     m++; 
    } 
    else 
    { 
     cr= Comproll1+Comproll2; 
     cp= cp+cr; 
    } 
1

問題是你需要檢查它們是否都是1,然後檢查它們是否都是1。如果我們看一下代碼:

if (Comproll1==1 || Comproll2==1) 
{ 
    System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!"); 
    cr= cr-cr; 
    m++; 
} 
else if (Comproll1==1 && Comproll2==1) 
{ 
    System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!"); 
    cp=cp-cp; 
    m++; 
} 

如果:

Comproll1 = 1

Comproll2 = 1

你期望它會進入else if (Comproll1==1 && Comproll2==1)但是,如果這是不是真正的if (Comproll1==1 || Comproll2==1)總是爲真。

爲了解決這個問題簡單地改變if S的順序是這樣的:

if (Comproll1==1 && Comproll2==1) 
{ 
    System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!"); 
    cp=cp-cp; 
    m++; 
} 
else if (Comproll1==1 || Comproll2==1) 
{ 
    System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!"); 
    cr= cr-cr; 
    m++; 
} 

希望這有助於:)

(你也需要重擲色子(如埃利奧特·弗裏施說,他的回答))