-2
我有一个卡类,看起来像这样:toString方法
public class Card
{
//instance variables
private String faceValue; //the face value of the card
private String suit; //the suit of the card
String[] ranks = {"Ace", "2", "3", "4", "5", "6","7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
/**
* Constructor
*/
public Card()
{
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
faceValue = ranks[i];
suit = suits[j];
}
}
}
//getters
/**
* Getter for faceValue.
*/
public String getFaceValue()
{
return faceValue;
}
/**
* Getter for suit.
*/
public String getSuit()
{
return suit;
}
//end of getters
//methods
/**
* This method returns a String representation of a Card object.
*
* @param none
* @return String
*/
public String toString()
{
return "Dealed a card: " + faceValue + " of " + suit;
}
}
和使用该卡类来创建一个数组另一个甲板类:
public class Deck
{
//instance variables
private Card[] deck;
/**
* Constructor for objects of class Deck
*/
public Deck()
{
deck = new Card[52];
}
/**
* String representation.
*/
public String toString()
{
return "Dealed a card: " + deck.getFaceValue() + " of " + deck.getSuit();
}
}
我的toString方法是给我错误“无法找到符号 - 方法getFaceValue()”。相同的getSuit()。任何想法为什么?
'deck'是'Card'阵列,而不是'Card'。数组没有这两种方法。 – resueman
数组没有方法。数组的元素有方法。 – bmargulies
您可以简单地返回'Arrays.toString(deck)',它将列出所有卡片的字符串。 –