2014-01-29 82 views
-1

我試圖實現一個擁有5張牌陣列列表的撲克牌手類,並且我需要檢查是否有任何重複的牌。這是代碼,但它給了我一個錯誤消息說在最後一條if語句中:返回類型丟失!我不知道那是什麼意思。這是一個if語句。爲什麼我需要返回類型?!比較撲克牌中的兩張牌

import java.util.ArrayList; 

public class Pokerhand { 

    public final int CARDS_NUMBER = 5; 
    private ArrayList<Card> cards = new ArrayList<Card>(); 

    public Pokerhand(Card card1, Card card2, Card card3, Card card4, Card card5) { 
     cards.add(card1); 
     cards.add(card2); 
     cards.add(card3); 
     cards.add(card4); 
     cards.add(card5); 
    } 

    private boolean checkCorrectness(ArrayList<Card> cards) { 
     if (cards.size() != CARDS_NUMBER) 
     throw new IllegalArgumentException("Incorrect number of cards!! "); 

     for (int i = 0; i < cards.size() - 1; i++) { 
     for (int j = i + 1; j < cards.size(); j++) { 
      if (cards.get(j).equals(cards.get(j + 1))) 
       throw new IllegalArgumentException("Duplicat card"); 

     } 
     } 

     return true; 
    } 

    if (checkCorrectness (cards)) 
     this.cards = cards; 
} 
+0

因爲否則你沒有返回任何東西,你說該方法返回一個布爾值。 – AntonH

+0

@Andreas是..這是我的實際代碼.. –

+0

@Andreas ..最後一條if語句被紅色突出顯示,並且錯誤消息顯示:該方法的返回類型丟失 –

回答

0

如果你不打算在函數來處理異常,你應該明確地標記功能throws IllegalArgumentException

例子:

private boolean checkCorrectness(ArrayList<Card> cards) throws IllegalArgumentException 

否則,你應該考慮切換return-type可以給你更多的信息,如intenum(最好),並返回錯誤代碼。

如果我們選擇int不是考慮這個問題:

-1 = "Incorrect number of cards" 
-2 = "Duplicate card" 

例子:

private int checkCorrectness(ArrayList<Card> cards) 
+1

我仍然不明白這個布爾返回類型有什麼問題?! –

+0

@EmanHamed因爲異常應該是_exceptional_,而不是標準的事件。 – AJMansfield

1

你有你的括號搞砸了:最後兩個語句看起來就像是class範圍之內,但你在類範圍內不能有if聲明:

} // end checkCorrectness(...) 

    if (checkCorrectness (cards)) 
     this.cards = cards; 
} // end class 

重要規則:始終正確格式化您的代碼,包括合理的縮進 - 這使得更容易發現此類問題。

你大概意思檢查卡的正確性,他們已經在構造函數中添加後 - 然後,取出上述兩行,做這樣的事情,而不是:

public Pokerhand(Card card1, Card card2, Card card3, Card card4, Card card5) { 
    cards.add(card1); 
    cards.add(card2); 
    cards.add(card3); 
    cards.add(card4); 
    cards.add(card5); 

    checkCorrectness (cards); // throws exception if the card definition is wrong 
} 

這應該解決您的與您在問題中顯示的代碼直接相關的問題。請參閱@ AJMansfield關於如何改進Pokerhand API的答案 - 構造函數中的參數太多通常是一些糟糕的API設計。

+0

爲什麼?!那麼如果我想調用方法來檢查卡的正確性,該怎麼辦? –

+0

您只能從另一種方法中調用方法。在運行程序時調用的Java中有一個特殊的方法'public static void main(String [] args)'。所有其他方法都可以從其他方法調用,包括可能的'main'。 – dcsohl

2

請您先解決一堆樣式問題,甚至在評估您的邏輯之前。一旦做到這一點,有邏輯的問題,應該成爲顯而易見的:

import java.util.ArrayList; 

/** 
* Represents a hand of cards in a game of Poker. 
*/ 
public class Pokerhand { 

    /** 
    * The number of cards in a valid poker hand. Hands with other numbers of 
    * cards are not considered valid. 
    */ 
    public final int HAND_SIZE = 5; 

    private Card [] cards; 

    /** 
    * Constructs a new hand containing the given cards. 
    * @param cards The cards this hand is to contain. 
    * @throws InvalidArgumentException if the cards passed do not form a valid 
    *  hand. A hand is considered valid if there are exactly 
    *  {@code HAND_SIZE} cards, and no repeated cards. 
    * @see Pokerhand#isValidHand 
    */ 
    public Pokerhand(Card ... cards) throws InvalidArgumentException { 
     if(isValidHand(cards)) 
      this.cards = cards; 
     else 
      throw new InvalidArgumentException("Bad hand"); 
    } 

    /** 
    * Checks if the cards passed together form a valid hand. A hand is 
    * considered valid if there are exactly {@code HAND_SIZE} cards, and no 
    * repeated cards. 
    * @param cards The cards to check 
    * @return {@code true} if the cards form a valid hand, 
    *   {@code false} otherwise. 
    */ 
    public boolean isValidHand(Card ... cards){ 
     if (cards.length != HAND_SIZE) 
      return false; 

     for(int i=0; i < cards.size()-1; i++) 
      for(int j=i+1; j < cards.size(); j++) 
       if(cards[j].equals(cards[j+1])) 
        return false; 

     return true; 
    } 
} 

推理更改:

  • 一切都應該有一個javadoc。
  • checkCorectness對於這種方法來說是一個非常糟糕的名字。應該命名一個boolean方法,以便其返回值所表示的狀態立即顯而易見。作爲一個更極端的例子,你期望checkIfNotX(x)返回什麼?該方法應該被命名爲它返回的內容,而不是它所做的。 isValidHand是一個更好的名字。
  • isValidHand應該是public,因爲班級的用戶可能希望遵循「先檢查」模式而不是「請求寬恕」模式。
  • isValidHand不應該拋出異常,因爲它應該是public
  • cards應該是一個數組,因爲你沒有用它們做任何事情都需要List的任何動態能力。
  • isValidHandPokerhand應採用可變參數,因爲這種方式更加透明,並且使它們更容易使用。

現在,你可以看到,誤差在isValidHand嵌套for環內對if聲明。你的編譯器應該給你一個警告,說從未使用過i。條件應爲cards[i].equals(cards[j])