2016-10-08 23 views
0

正試圖找到一種在五張牌撲克牌中的四種。但它不工作,不知道爲什麼。五手牌遊戲,想不通,badugi&四種一種

public boolean hasFourOfaKind(String hand) { 
     int counter = 0; 
     char x = 0; 

     for (int i = 0; i < hand.length(); i++) 
     { 
      if (i == 0) { 
       x = hand.charAt(0); 
      } else if (x == hand.charAt(i)) { 
       counter++; 

      } 
     } 
     if (counter >= 4) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

這裏同樣的問題,我試圖檢查給定四卡手是否是一個的Badugi

public boolean hasFourCardBadugi(String hand) { 
     int diffcounter = 0; 
     char badugi = 0; 

     for (int i = 0; i < hand.length(); i++) { 
      if (i == 0) { 
       badugi = hand.charAt(0); 
      } else if (badugi != hand.charAt(i)) { 
       diffcounter++; 
      } 
     } 
     if (diffcounter >= 10) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
+0

那究竟是什麼問題呢?你能舉一個你給的輸入的例子,並得到錯誤的輸出嗎? – Mureinik

+2

將數據表示爲字符串中的字符並不好,java是一種面向對象的語言,這意味着您有像類和對象這樣的概念,可以更好地完成這項工作。 – Tobb

+0

正在使用給定的junit來測試它。 – Heniam

回答

0

讓我們來看看你的for循環。

for (int i = 0; i < hand.length(); i++) 
    { 
     if (i == 0) { 
      x = hand.charAt(0); 
     } else if (x == hand.charAt(i)) { 
      counter++; 
     } 
    } 

在這部分

 if (i == 0) { 
      x = hand.charAt(0); 
     } 

你設置X到的第一張牌。但你永遠不會指望那張牌。您需要添加:

 if (i == 0) { 
      x = hand.charAt(0); 
      counter++; 
     } 

當然,這還是有它不會檢測手,其中一種的四個不第一卡匹配問題(ACE二兩二兩),但你應該能夠現在就解決這個基本錯誤。一種方法可能只涉及第二個循環。

+0

謝謝您也有與badugi相同的問題。 – Heniam

+0

@Heniammm不...在那個你設置diffcounter爲10,然後檢查diffcoutner> = 10,這將永遠是真實的。請注意並接受這個,如果它幫助=] – nhouser9