Java的酒杯卡总计数

问题描述:

这里是低于我的手类的全部:Java的酒杯卡总计数

,我有是,当我运行程序时,它给了我喜欢输出的问题:

“欢迎酒杯...打交道2卡:

这是你的卡:[AH,3S]
10(H),或(S)t和 “

一颗心和一个黑桃3不应该给我10.我不知道我的代码有什么问题。我getSoftTotal()方法看起来像它应该工作正常(我还没有与我getHardTotal()方法困扰尚未

下面是另一个输出:

“这是你的卡:[4D,10D]
25, (H),或(S)t和?“

import java.util.ArrayList; 
import java.util.Scanner; 

public class Hand extends Deck { 

    private static ArrayList<Card> cards; 

    public Hand() { 

     cards = new ArrayList<Card>(); 
    } 

    public void addCard(Card other) { 

     cards.add(other); 
    } 

    public static boolean hasBlackjack() { 

     boolean ace = true; 
     boolean ten = true; 

     for (int i = 0; i < cards.size(); i++) { 
      if (!(cards.get(i).getValue() == Card.ACE)) 
       ace = false; 
      if (!(cards.get(i).getValue() == 10)) 
       ten = false; 
     } 
     return (ace && ten); 
    } 

    public static boolean isBusted() { 
     return getTotal() > 21; 

    } 

    public static int getSoftTotal() { 
     int total = 0; 
     for (Card card : cards) { 
      total += card.getValue(); 
     } 
     return total; 
    } 

    public static int getTotal() { 

     return getSoftTotal(); // soft 
    } 

    @SuppressWarnings("static-access") 
    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     Deck deck; // A deck of cards. A new deck for each game. 
     String hit = ""; 

     Hand hand = new Hand(); 
     deck = new Deck(); 
     deck.shuffle(); 

     System.out.println("Welcome to blackjack...dealing 2 cards:"); 
     System.out.println(""); 

     hand.addCard(deck.deal()); 
     hand.addCard(deck.deal()); 

     do { 
      System.out.println("Here are your cards: " + cards.toString()); 
      hand.addCard(deck.deal()); 
      System.out.println(hand.getTotal() + " , (h)it or (s)tand? "); 
      System.out.println(""); 
      hit = input.nextLine(); 
     } while (hit.equals("h") && isBusted() == false 
       && hasBlackjack() == false); 

     if (isBusted() == true) { 
      System.out.println("BUSTED! YOU LOSE!"); 
     } 
     if (hasBlackjack() == true) { 
      System.out.println("BLACKJACK! YOU WIN!"); 
     } 
     if (hit.equals("s") && isBusted() == false) { 
      System.out.println("You ended with " + getTotal()); 
     } 
    } 

} 

Card.java

public class Card { 

public final static int CLUBS = 0; 
public final static int HEARTS = 1; 
public final static int SPADES = 2; 
public final static int DIAMONDS = 3; 

// Numbers for the values 
public final static int ACE = 1; 
public final static int JACK = 11; 
public final static int QUEEN = 12; 
public final static int KING = 13; 

/** 
* This card's suit, one of the constants SPADES, HEARTS, DIAMONDS, CLUBS, 
* or JOKER. The suit cannot be changed after the card is constructed. 
*/ 
private final int suit; 

/** 
* The card's value. For a normal cards, this is one of the values 1 through 
* 13, with 1 representing ACE. For a JOKER, the value can be anything. The 
* value cannot be changed after the card is constructed. 
*/ 
private final int value; 

/** 
* Creates a card with a specified suit and value. 
* 
* @param theValue 
*   the value of the new card. For a regular card (non-joker), the 
*   value must be in the range 1 through 13, with 1 representing 
*   an Ace. You can use the constants Card.ACE, Card.JACK, 
*   Card.QUEEN, and Card.KING. For a Joker, the value can be 
*   anything. 
* @param theSuit 
*   the suit of the new card. This must be one of the values 
*   Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or 
*   Card.JOKER. 
* @throws IllegalArgumentException 
*    if the parameter values are not in the Permissible ranges 
*/ 
public Card(int cardValue, int cardSuit) { 
    if (cardSuit > 3 || cardSuit < 0) 
     throw new IllegalArgumentException("Illegal card suit:" + cardSuit); 
    if (cardValue > 13 || cardValue < 1) 
     throw new IllegalArgumentException("Illegal card value:" 
       + cardValue); 
    value = cardValue; 
    suit = cardSuit; 
} 

public int getSuit() { 
    return suit; 
} 

public int getValue() { 
    return value; 
} 

public String getSuitString() { 
    switch (suit) { 

    case CLUBS: 
     return "C"; 
    case HEARTS: 
     return "H"; 
    case SPADES: 
     return "S"; 
    case DIAMONDS: 
     return "D"; 
    default: 
     return ""; 
    } 
} 

/** 
* Returns a String representation of the card's value. 
* 
* @return for a regular card, one of the strings "Ace", "2", "3", ..., 
*   "10", "Jack", "Queen", or "King". For a Joker, the string is 
*   always numerical. 
*/ 
public String getValueString() { 

    switch (value) { 
    case 1: 
     return "A"; 
    case 2: 
     return "2"; 
    case 3: 
     return "3"; 
    case 4: 
     return "4"; 
    case 5: 
     return "5"; 
    case 6: 
     return "6"; 
    case 7: 
     return "7"; 
    case 8: 
     return "8"; 
    case 9: 
     return "9"; 
    case 10: 
     return "10"; 
    case 11: 
     return "J"; 
    case 12: 
     return "Q"; 
    case 13: 
     return "K"; // Default will return King if it's not any of those up 

    default: 
     return ""; 

    } 
} 

public String toString() { 

    return getValueString() + "" + getSuitString(); 
} 

public boolean equals(Card other) { 

    if (this.value == other.value && this.suit == other.suit){ 
     return true;} 
    else{ 

     return false; 
    } 
} 

public static void main(String[] args) { 

    Card a = new Card(10, 3); 
    System.out.println(a.toString()); 

    // Card b = new Card(14, 3); //ThrowsIllegalArgumentException 
    // System.out.println(b.toString()); 

    Card c = new Card(1, 2); 
    System.out.println(c.toString()); 

    Card equals1Yes = new Card(2, 3); 
    Card equals2Yes = new Card(2, 3); 

    Card notEquals = new Card(3, 2); 

    System.out.println(equals1Yes.equals(equals2Yes)); 
    System.out.println(notEquals.equals(equals1Yes)); 

    System.out.println(a.getSuit()); 
    System.out.println(a.getValue()); 

    System.out.println(a.getValueString()); 
    System.out.println(a.getSuitString()); 





} 

} // end class Card 

Deck.java

import java.util.*; 

public class Deck { 

private ArrayList<Card> cards; 

public Deck() { 
    cards = new ArrayList<Card>(); 

    for (int a = 1; a <= 13; a++) { 
     for (int b = 0; b <= 3; b++) { 
      cards.add(new Card(a, b)); 
     } 
    } 

} 

public Card deal() { 
    if (cards.size() != 0) { 
     return cards.remove(0); 
    } else { 
     return null; 
    } 

} 

public String toString() { 
    String return1 = ""; 
    for (int i = 0; i <= cards.size(); i++) { 
     return1 = +cards.indexOf(i) + " "; 
    } 
    return return1; 

} 

public int size() { 
    return cards.size(); 
} 

public void shuffle() { 

    Collections.shuffle(cards); 
} 

public boolean isEmpty() { 
    if (cards.size() == 0) { 
     return true; 
    } else { 
     return false; 
    } 

} 

public void reset() { 

} 

public static void main(String[] args) { 



} 
} 
+0

我们需要看看你的卡类。 –

+0

我已经添加了卡类。 – user2443505

+0

对不起,看起来我们也需要看'Deck'类,因为这显然是你为卡分配值的地方。 –

bug是你Hand主要方法。在你手中显示最初的两张牌之后,你正在处理一张额外的牌。

System.out.println("Here are your cards: " + cards.toString()); 
hand.addCard(deck.deal()); 

这张额外卡的价值已包含在下一行的手牌总值中。我认为你的代码将(主要)工作,如果你删除上面的第二行。


附加说明:我觉得有些你的双手将根据二十一点规则,因为你必须在你的显卡型号设置不正确的面对卡值计算错误。杰克,女王和国王应该都是10的值。

+0

谢谢你的回应! (对不起,你必须阅读所有的代码才能看到这样的简单错误。) 此外,对于我的程序,我们使用不同的值(但感谢警告)。 – user2443505

+0

@ user2443505当然,没问题。 –

您比Bill指出的问题还多。您的hasBlackjack()getTotal()函数都是错误的,并且您没有区分硬总数和软总数,这将是继续前进的必要条件。之后可能会有更多,但一次只有一个错误...此外,从不压制警告。他们在那里是有原因的。如果您收到警告,请修正代码,请勿压制。

这里有一个工作hasBlackjack()。我会给你修正getTotal(),但你的第一个暗示是卡片值10,11,12和13都应该增加10。与你的班级一起,value > 9可用于检查10张卡片。

public static boolean hasBlackjack() { 
    if (cards.size() != 2) return false; 
    return ((cards.get(0).getValue() == Card.ACE && cards.get(1).getValue() > 9) || 
     (cards.get(1).getValue() == Card.ACE && cards.get(0).getValue() > 9)); 
}