public enum Suit
{
CLUBS,
HEARTS,
SPADES,
DIAMONDS
}
public enum Value
{
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE
}
Card.javaJava的邏輯循環//試圖創建卡的甲板
public class Card {
private Suit suit;
private Value value;
public Card(Suit theSuit, Value theValue)
{
suit = theSuit;
value = theValue;
}
public String toString()
{
return value + " of " + suit;
}
public Value getValue()
{
return value;
}
public Suit getSuit()
{
return suit;
}
public boolean equals(Card other)
{
if (value.ordinal() == other.value.ordinal()
|| suit.ordinal() == other.suit.ordinal())
{
return true;
}
else {
return false;
}
}
}
CardPile.java
public class CardPile
{
public Card[] cards;
private int numCards;
public CardPile()
{
this.cards = new Card[52];
this.numCards = 0;
// The problem is here, when I try to iterate through the enums and the
// array to populate my cards[] of 52 objects Card it populates it with
// 52 Card which are all ACE of DIAMONDS, it looks like the triple loops
// populates 52 times the two last elements of my enum, but I can't
// figure out how to fix that! Thanks in advance!
for (Suit s : Suit.values())
{
for (Value v : Value.values())
{
for (int π = 0; π < cards.length; π++)
{
cards[π] = new Card(s, v);
}
}
}
}
public boolean isEmpty()
{
for (int i = 0; i < cards.length; i++)
{
if (cards[i] != null)
{
return false;
}
}
return true;
}
public int getNumCards()
{
return numCards;
}
}
你真的用'π'? – sp00m 2013-03-20 17:01:15
我注意到,失蹤for循環這裏之前對(價值:v值())for循環有一個爲(套裝:的價值觀())循環,包裹2個循環!謝謝! – user2058291 2013-03-20 17:01:25
(1)你的for循環在註釋中丟失了,並沒有被格式化爲代碼(2)使用pi丟失了for循環,這是不需要的。 – splungebob 2013-03-20 17:03:41