[英]How do I assign more than one of the same String in a loop?
我正在嘗試創建16張(4x4)卡的網格。 總共有8張不同的卡片,因此每種卡片類型都會重復兩次。
private static int rows = 4;
private static int columns = 4;
public static Card[][] card = new Card[rows][columns];
public String[][] printHiddenCard() {
for(int i = 0; i < card.length; i++){
for(int j = 0; j < card[i].length; j++){
card[i][j] = new QCard();
}
}
}
我不知道如何在每個QCard
插入兩個,即“?”之一。 或我的其他7種類型的卡片(+,-,%,/等)以隨機順序放入對象數組。
我將列出16張卡,然后將其洗牌。
// c1, c2, c3, c4, c5, c6, c7, c8 are your 8 different Cards.
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 k = 0;
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
card[i][j] = list.get(k++);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.