嗨,我正在尝试练习Java,并且刚刚开始! 为了练习,我试图构建一个简单的扑克游戏,但被卡牌数组列表卡住了。我试图将其打印出来,但仅将地址输出到控制台,自然地,我尝试使用.toString方法并创建自己的toString方法来尝试检查数组列表是否输入正确。贝娄是我的Card类,带有Arraylist {Card} Deck。波纹管是我的密码
package poker;
import java.util.ArrayList;
public class Card {
private String _suit;
private int _cardNum;
public Card(int cardNum, String suit) {
_suit = suit;
_cardNum = cardNum;
}
public String toString() {
return _suit +_cardNum;
}
static ArrayList<`enter code here`Card> Deck = new ArrayList<Card>() {{
add(new Card(0,"Hearts"));
add(new Card(1,"Hearts"));
add(new Card(1,"Hearts"));
add(new Card(2,"Hearts"));
add(new Card(3,"Hearts"));
add(new Card(4,"Hearts"));
add(new Card(5,"Hearts"));
add(new Card(6,"Hearts"));
add(new Card(7,"Hearts"));
add(new Card(8,"Hearts"));
add(new Card(9,"Hearts"));
add(new Card(10,"Hearts"));
add(new Card(11,"Hearts"));
add(new Card(12,"Hearts"));
add(new Card(13,"Hearts"));
add(new Card(0,"Spades"));
add(new Card(1,"Spades"));
add(new Card(2,"Spades"));
add(new Card(3,"Spades"));
add(new Card(4,"Spades"));
add(new Card(5,"Spades"));
add(new Card(6,"Spades"));
add(new Card(7,"Spades"));
add(new Card(8,"Spades"));
add(new Card(9,"Spades"));
add(new Card(10,"Spades"));
add(new Card(11,"Spades"));
add(new Card(12,"Spades"));
add(new Card(13,"Spades"));
add(new Card(0,"Clubs"));
add(new Card(1,"Clubs"));
add(new Card(2,"Clubs"));
add(new Card(3,"Clubs"));
add(new Card(4,"Clubs"));
add(new Card(5,"Clubs"));
add(new Card(6,"Clubs"));
add(new Card(7,"Clubs"));
add(new Card(8,"Clubs"));
add(new Card(9,"Clubs"));
add(new Card(10,"Clubs"));
add(new Card(11,"Clubs"));
add(new Card(12,"Clubs"));
add(new Card(13,"Clubs"));
add(new Card(0,"Diamonds"));
add(new Card(1,"Diamonds"));
add(new Card(2,"Diamonds"));
add(new Card(3,"Diamonds"));
add(new Card(4,"Diamonds"));
add(new Card(5,"Diamonds"));
add(new Card(6,"Diamonds"));
add(new Card(7,"Diamonds"));
add(new Card(8,"Diamonds"));
add(new Card(9,"Diamonds"));
add(new Card(10,"Diamonds"));
add(new Card(11,"Diamonds"));
add(new Card(12,"Diamonds"));
add(new Card(13,"Diamonds"));
}};
}
和我的MainApp
package poker;
import java.util.Random;
import static java.lang.System.out;
public class MainApp {
public static void main(String[] args) {
for(int i = 0;i <Card.Deck.size();i++) {
out.println(Card.toString());
}
}
}
在打印行出现问题的地方。如何将arraylist从类类型转换为字符串以进行输出? 预先感谢!
答案 0 :(得分:1)
由于toString()
不是静态方法,因此需要在类的实例上调用它。因此,在您的MainApp中将行out.println(Card.toString());
更改为out.println(Card.Deck.get(i).toString());
。
在旁注中,我注意到您每套衣服有14张卡,我认为这是无意的。
答案 1 :(得分:0)
您可以尝试下面的方法,它应该打印ArrayList的所有String内容。
System.out.println(Card.Deck);
类似
[Hearts0,Hearts1,Hearts1,Hearts2,Hearts3,Hearts4,Hearts5, Hearts6,Hearts7,Hearts8,Hearts9,Hearts10,Hearts11,Hearts12, Hearts13,Spades0,Spades1,Spades2,Spades3,Spades4,Spades5, Spades6,Spades7,Spades8,Spades9,Spades10,Spades11,Spades12, 黑桃13,俱乐部0,俱乐部1,俱乐部2,俱乐部3,俱乐部4,俱乐部5,俱乐部6, 俱乐部7,俱乐部8,俱乐部9,俱乐部10,俱乐部11,俱乐部12,俱乐部13,钻石0, 钻石1,钻石2,钻石3,钻石4,钻石5,钻石6, Diamonds7,Diamonds8,Diamonds9,Diamonds10,Diamonds11,Diamonds12, 钻石13]
答案 2 :(得分:0)
您看到的错误是什么?
我看到的最大问题是您的for循环没有使用索引来取消引用Deck数组的成员:
for(int i = 0;i <Card.Deck.size();i++) {
out.println(Card.toString());
}
卡是类-不是对象。您已经创建了Card类的一堆对象,但这不是您在该代码中引用的对象。要引用对象实例而不是类,请执行以下操作:
你应该更换
Card.toString()
与:
Card.Deck.get(i).toString()
-但是有一种更好的方法,请参见下文。
顺便说一句,您应该在toString的实现中使用@Override属性。可能并非严格必要,但可以清楚地说明您正在覆盖默认实现:
@Override
public String toString() {
return _suit +_cardNum;
}
如上所述,有一种更简单的方法来打印牌组中的每张卡片:使用迭代器快捷键for
而不是将arraylist视为数组:
public void printDeck() {
for(Card card : Card.Deck) {
out.print(card.toString());
}
}
答案 3 :(得分:0)
您的Card.toString()未引用Deck对象中的Card。您要做的是从Deck对象中打印Card的字符串值。如果您使用的是Java 8,则可以使用以下代码:
Card.Deck.forEach(card -> System.out.println(card.toString()));