gpt4 book ai didi

java - 我无法正确打印我的卡片对象

转载 作者:行者123 更新时间:2023-11-30 07:50:45 26 4
gpt4 key购买 nike

这是我尝试显示的方法,它应该是 4x4 的卡片网格。我觉得卡片对象内部可能有问题?这是当我尝试运行 printHiddenCard 中的内容时得到的结果。

edu.cpp.cs.cs141.memgame.QCard@182decdb 
edu.cpp.cs.cs141.memgame.StarCard@26f0a63f
edu.cpp.cs.cs141.memgame.PercCard@4361bd48
edu.cpp.cs.cs141.memgame.PercCard@4361bd48
edu.cpp.cs.cs141.memgame.MinusCard@53bd815b
edu.cpp.cs.cs141.memgame.MinusCard@53bd815b
edu.cpp.cs.cs141.memgame.PoundCard@2401f4c3
edu.cpp.cs.cs141.memgame.QCard@182decdb
edu.cpp.cs.cs141.memgame.SlashCard@7637f22
edu.cpp.cs.cs141.memgame.ExclCard@4926097b
edu.cpp.cs.cs141.memgame.StarCard@26f0a63f
edu.cpp.cs.cs141.memgame.PlusCard@762efe5d
edu.cpp.cs.cs141.memgame.PlusCard@762efe5d
edu.cpp.cs.cs141.memgame.SlashCard@7637f22
edu.cpp.cs.cs141.memgame.ExclCard@4926097b
edu.cpp.cs.cs141.memgame.PoundCard@2401f4c3



private static int rows = 4;
private static int columns = 4;
public static Card[][] card = new Card[rows][columns];

public Card[][] printHiddenCard() {

QCard c1 = new QCard();
StarCard c2 = new StarCard();
MinusCard c3 = new MinusCard();
PoundCard c4 = new PoundCard();
ExclCard c5 = new ExclCard();
PercCard c6 = new PercCard();
SlashCard c7 = new SlashCard();
PlusCard c8 = new PlusCard();

List<Card> allCards = Arrays.asList(c1, c2, c3, c4, c5, c6, c7, c8);
List<Card> list = new ArrayList<>(allCards);
list.addAll(allCards);
Collections.shuffle(list);

int counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
card[i][j] = list.get(counter++);
System.out.println(card[i][j] + " ");
}
}
return card;
}

这是其中一个卡片对象的样子:

public class QCard extends Card {

public QCard() {
super("?");
}

}

这是父类(super class):

public abstract class Card {

private String cardType = "";
private boolean isFlipped = false;

public Card(String cardType) {
this.cardType = cardType;
}

public String getCardType() {
return cardType;
}

public boolean isFlipped() {
return isFlipped;
}

public void setFlipped(boolean isFlipped) {
this.isFlipped = isFlipped;
}

}

最佳答案

您应该重写要打印的对象中的 toString() 方法,如下所示:

public class QCard extends Card {
public QCard() {
super("?");
}

public String toString() {
return "card data";
}
}

上面的示例将始终打印卡片数据,因此请添加您想要包含在打印输出中的字段。

关于java - 我无法正确打印我的卡片对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com