// **下面的代碼被分成2個類別和2枚枚舉:Card,Deck,Suit和Rank。卡片卡片java項目混淆了一個for循環和toString方法
我在上纏繞一個for循環和toString方法要求在說明書的困惑:
創建一個在甲板類的toString()方法,使用另一個for循環打印所有的卡在陣列中通過調用每個卡上的toString()方法。此方法「要求」卡片僅通過在卡片對象上調用toString()來打印其等級和套裝。這種方法在任何地方都不應該提及等級或適合度。讓Card的toString()方法完成工作(委託)。
public class Card {
public static void main(String[] args) {
Card myCard = new Card();
System.out.println(myCard.toString());
System.out.println(new Card(Rank.KING, Suit.CLUBS));
}
public Rank rank;
public Suit suit;
public Rank getRank() {
return rank;
}
private void setRank(Rank rank) {
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
private void setSuit(Suit suit) {
this.suit = suit;
}
// Constructor assigning attributes to rank and suit
public Card(Rank rank, Suit suit) {
setRank(rank);
setSuit(suit);
}
public Card() {
this(Rank.ACE, Suit.SPADES);
}
@Override
public String toString() {
// TODO Auto-generated method stub
String result= rank.toString() + " of " + suit.toString();
return result; }
}
public enum Suit {
NONE, CLUBS, HEARTS, SPADES, DIAMONDS
}
public enum Rank {
JOKER, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN,
KING, ACE
}
public class Deck {
public void main(String[] args) {
int[] DeckArray = new int[54];
int count = 2;
System.out.println(count);
for (count = 0; count <= 54; count++)
{
System.out.println(DeckArray);
}
}
public Card cards[];
public void DeckOfCards() {
this.cards = new Card[52];
Card myCard = new Card();
for (int i = 0; i < 0; i++) {
Card card = new Card(Rank.ACE, Suit.CLUBS);
if (Rank.JOKER != null) {
System.out.println("JOKER");
} else {
System.out.println(myCard);
}
}
for (int i = 0; i < 52; i++) {
String Deck = Arrays.toString(DeckArray)
}
}
}
小心:'計數<= 54'在'Deck'將導致異常時'的System.out.println(DeckArray [COUNT]);'將用'count = 54'調用。順便說一下,你必須用'System.out.println(DeckArray [count])替換'System.out.println(DeckArray);'並且請格式化你的代碼 –
你究竟在想什麼?你能更具體地瞭解你的困惑嗎?另外,這是你的所有代碼?沒有任何內容「if(Rank.JOKER)」 – sjgp
請注意正確格式化你的問題,因爲它是不可讀的(尤其是代碼)。 –