2014-11-05 94 views
1

對於我的代碼,我想檢查一個數組,它將用6個隨機整數多次填充。 每次我想檢查是否所有6個值都不同,並且這是否代碼停止。 但嘗試使用循環後,我遇到了問題,所以我只用了一個長長的屁股If語句來檢查每個位置與陣列中的每個位置。但是這不起作用。如果有人能告訴我爲什麼我很愚蠢,會很樂意。檢查數組中的所有值是否不相等(java)

if((diceArray[0] != diceArray[1] & diceArray[0]!=diceArray[2] & diceArray[0]!=diceArray[3] & diceArray[0]!=diceArray[4] & diceArray[0]!=diceArray[5] & diceArray[1]!=diceArray[2] & diceArray[1]!=diceArray[3] & diceArray[1]!=diceArray[4] & diceArray[1]!=diceArray[5] & diceArray[2]!=diceArray[3] & diceArray[2]!=diceArray[4] & diceArray[2]!=diceArray[5] & diceArray[3]!=diceArray[4] & diceArray[3]!=diceArray[5] & diceArray[4]!=diceArray[5])) 

       { 
        System.out.println("stop babes"); 
       } 
       else 
       { 
        System.out.print("Did not work"); 
       } 

這個代碼還有更多,但我知道其餘的工作,因爲當我打印出每個序列,他們都沒有問題。當我用這部分代碼運行時理想情況它會打印出很多重複序列的序列,並且這些打印出來「沒有工作」,其中一個結果應該與「阻止寶貝」一起出來(這些將在稍後使用它們被刪除以測試和返回用於停止代碼)但是,當我運行代碼發生這種情況

how many rolls of 6 dice? 3 
LINE 1 
4 1 3 5 5 5 
stop babes 
LINE 2 
4 4 1 2 6 1 
stop babes 
LINE 3 
3 6 4 6 4 4 
stop babes 

只是在if語句快速編輯。我意識到它可以縮短爲一個快速循環,我只是想蠻橫的問題。 主要的問題是文本「停止辣妹」應該只打印代碼,如1 2 3 4 5 6打印告訴我的代碼工程,並允許我在主FOR循環中插入一個break語句。

+0

AND運算符&& – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2014-11-05 22:10:27

+4

儘管所有的答案,評論到目前爲止,'&'是一個有效的布爾/邏輯運算;它不會像'&&'那樣短路。 – GriffeyDog 2014-11-05 22:12:38

+0

我以前已經&&,但在排除故障時進行了更改。沒有工作 – RemoteCntrol 2014-11-05 22:15:11

回答

0

如果你最終的目標是去除有重複序列,你應該考慮檢查當時的數組,你往裏面:

而不是僅僅做這樣的事情(你沒有提供的代碼,所以我只是猜測):

n = rand.nextInt(); 
diceArray[nextPosition++] = n; 

做這樣的事情:

n = rand.nextInt(); 
for (int i = 0; i < nextPosition; i++) { 
    if (diceArray[i] == n) { 
     return; // Or whatever will cause the array to be disqualified. 
    } 
} 

這樣,你能救自己,你無論如何也不會使用滾動數字。假設你翻了2 2。你已經知道你必須取消這個陣列的資格,你不需要繼續滾動到2 2 1 3 4 5,然後遍歷整個事情並最終取消它的資格。

如果要算你有你有一個良好的陣列之前壞陣列的數量也是如此。

Random rand = new Random(); 
    int[] diceArray = new int[6]; 

    int badArrays = 0; 
    int nextPosition = 0; 

    while (nextPosition < 6) { 

     boolean goodMove = true; 
     int n = rand.nextInt(6) + 1; 
     for (int i = 0; i < nextPosition; i++) { 
      if (diceArray[i] == n) { 
       goodMove = false; 
       break; 
      } 
     } 
     if (goodMove) { 
      diceArray[nextPosition++] = n; 
     } else { 
      nextPosition = 0; 
      badArrays++; 
     } 
    } 

    System.out.println("Got the array: " + Arrays.toString(diceArray) 
         + " after " + badArrays + " failures."); 
+0

其實目的是要算多少次花了一段時間沒有任何重複。 – RemoteCntrol 2014-11-05 22:37:44

+0

非常好,它也適用於此。我編輯了我的答案,向您展示了用於計算重複數組數的代碼,但並未實際填充它們。 – RealSkeptic 2014-11-05 23:01:27

+0

你是我朋友的冠軍!這對我來說非常合適。非常類似於我正在做的事情,但更容易處理。很容易就能適應我需要的東西。我怎麼說問題回答?我是新的:D – RemoteCntrol 2014-11-05 23:42:46

0

考慮使用一個for循環做的,而不是一個巨大的這個if語句:

for (int i = 0; i < 6; i++) 
{ 
    for (int j = i + 1; j < 6; j++) 
    { 
     if (diceArray[i] == diceArray[j]) 
      System.out.println("Did not work!"); 
    }  
} 

此外,最好使用&&運營商邏輯與,而不是&,因爲&&短路 - 它將在條件的第一部分停止並返回false,而&將計算所有條件,因此效率較低。

+3

兩者都是合乎邏輯的。你的陳述是錯誤的。 – Turing85 2014-11-05 22:12:43

+1

我相當肯定'diceArray [0] == diceArray [0]'這將是內部循環的第一次迭代。 – clcto 2014-11-05 22:14:32

+0

你說得對,我只是修好了。 – 2014-11-05 22:14:59

-2

我相信你想& &與&

一種是按位操作,而另一個是合乎邏輯的。

+3

這是不正確的。在布爾上下文中,&是「正常」,而&&是「快速」,如果一個表達式爲「假」,則中斷。 – Turing85 2014-11-05 22:12:03

+0

不知道我錯在哪裏看到,即使Java文檔顯示一個按位,另一個是邏輯。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html – Wranorn 2014-11-05 22:14:18

+0

「*一個是按位,另一個是邏輯*」,但它們都代表AND運算符。對於*布爾*參數,一個是短路(&&),另一個不是(&)。 – Pshemo 2014-11-05 22:17:02

1

這應該爲你工作:

public static bool areValuesUnique(int[] values) 
{ 
    for(int i = 0; i < values.length; ++i) 
    { 
     for(int j = i + 1; j < values.length; ++j) 
     { 
      if(values[i] == values[j]) 
       return false; 
     } 
    } 
    return true; 
} 

而且你會使用它,像這樣與任何大小的數組工作的額外好處。

if(areValuesUnique(diceArray)) 
{ 

} 
else 
{ 

} 
1

我想你需要使用Set來驗證數組中是否有任何等於對象。您只需使用數組中包含的對象創建新的HashSet,然後驗證創建的集合的長度。如果長度等於數組的長度,則所有對象都不相同。

Set<YOUR_ARRAY_TYPE> set = new HashSet<YOUR_ARRAY_TYPE>(); 
set.addAll(Arrays.asList(YOUR_ARRAY)); 
if (set.size() == YOUR_ARRAY.length) 
{ 
    ... 
} 
else 
{ 
    ... 
} 
+0

使用套裝就像拿大錘打破堅果。 – Turing85 2014-11-05 22:40:32

+0

@ Turing85當然,您有權獲得您的意見。但是在性能不重要的情況下,利用像這樣的Set屬性可以產生易於閱讀和維護的代碼。這個答案中的代碼顯然是正確的(儘管比必要的更冗長);與使用'for'循環的解決方案相比,這需要更多的檢查來驗證。 – dnault 2014-11-05 23:20:48

+0

我從來沒有說過,代碼是不正確的,但爲了這個目的(這是一個試圖學習Java的人的練習),只是有點太多。和可讀性意見:) – Turing85 2014-11-05 23:49:47

0

另一種方式,有點兒有趣,使用位掩碼。這是我們過去如何推出的。

// watch out: only works for dice values 0-30 
public static boolean areDiceUnique(int[] diceArray) { 
    int mask=0; 
    for(int i=0; i<diceArray.length; i++) { 
    int orMask = 1<<diceArray[i]; 
    if((mask & orMask) != 0) 
     return false; 
    mask |= orMask; 
    } 
    return true; 
} 

,如果你只是尋找獨特擲骰而已,然後就用List<Integer>有1,2,3,4,5,6和while(list.size()>0)開始在rand.nextInt(list.size())獲得該項目。

+0

的問題參見:http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html – dnault 2014-11-05 23:27:37

相關問題