【发布时间】:2022-01-22 00:02:33
【问题描述】:
package com.test.game;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Card {
private static String[] colours = new String[]{"E", "L", "H", "S"};
private static String[] cardValues = new String[]{"7", "8", "9", "10", "B", "D", "K", "A"};
private String cardValue;
private String colour;
private Card(String cardValue, String colour) {
this.cardValue = cardValue;
this.colour = colour;
}
public String toString() {
return cardValue + colour;
}
static void CardDeck() {
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i < colours.length; i++) {
for (int j = 0; j < cardValues.length; j++) {
cards.add(new Card(cardValues[j], colours[i]));
}
}
System.out.println(cards);
}
static void Collections(ArrayList<Card> cards, int seed){
Collections.shuffle(cards, new Random(seed));
System.out.println(cards);
}
public static void main(String[] args) {
System.out.println();
}
}
package com.test.game;
import java.util.ArrayList;
import java.util.Random;
public class Game {
public static void main(String[] args) {
Card.CardDeck();
Card.Collections();
}
}
所以我现在正在开发一款纸牌游戏。第一个类在 CardDeck() 方法的帮助下创建了一个包含卡片的数组列表,该方法在 Game 类中被调用,并且运行良好。现在在 Method Collections() 中,这个数组列表应该被打乱了。这样卡片的顺序是随机的。
因此,我有 2 个问题。首先是我洗牌的方式对吗?我怎么能在另一个类中调用这个Collectinons()method?由于它有参数,所以它不起作用。我发现了一些类似的问题,但它们并没有真正为我工作。 (创建一个新实例)
有人可以帮忙吗?
【问题讨论】:
标签: java list arraylist methods parameters