这是我创建卡片的地方
package javaapplication21;
公共类卡{
private String Name = null;
private String Description = null;
private String Type = null;
public Card(int i, int j) {
if (i == 0) {
Type = "Spades";
} else if (i == 1) {
Type = "Diamonds";
} else if (i == 2) {
Type = "Hearts";
} else if (i == 3) {
Type = "Clubs";
}
int Value = j;
if (j == 11) {
Name = "Jack";
} else if (j == 12) {
Name = "Queen";
} else if (j == 13) {
Name = "King";
} else if (j == 1) {
Name = "Ace";
} else if (j == 14) {
Name = "Joker";
} else {
Name = "" + Value;
}
}
public String getDescription(){
Description="Its a"+Name+"of"+Type;
return this.Description;
}
}
package javaapplication21;
import java.util.ArrayList;
public class JavaApplication21 {
public static void main(String[] args) {
ArrayList Deck=new ArrayList<Card>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 15; j++) {
new Card(i, j);
Deck.add(new Card(i, j));
}
}
System.out.println(Deck.get(5).getDescription());
}
}
尝试在卡组的索引5中使用卡对象的getDescription时,出现错误(无法找到符号)。我是编程新手,但我真的不知道是什么问题。
答案 0 :(得分:1)
get()
ArrayList
类Object
类返回对象的方法Object
类没有getDescription()
您应该将其投放到Card
班级 - (Card)Deck.get(5)
答案 1 :(得分:1)
arraylist不知道存储对象的类型。你必须定义类型。
ArrayList<Card> deck = new ArrayList<Card>();