开发提示:
参考答案:
public class Test6 {
public static void main(String[] args) {
int n = 5;
ArrayList<Card> cards = randomCard(n);
if (cards != null) {
System.out.println("随机"+ n +"张牌:" );
for (int i = 0; i < cards.size(); i++) {
Card card = cards.get(i);
card.showCard();
}
}else {
System.out.println(n+"超越范围,无法获取牌" );
}
System.out.println();
System.out.println();
int n2 = 55;
ArrayList<Card> cards2 = randomCard(n2);
if (cards2 != null) {
System.out.println("随机"+ n2 +"张牌:" );
for (int i = 0; i < cards.size(); i++) {
Card card = cards.get(i);
card.showCard();
}
}else {
System.out.println("随机"+ n2 +"张牌:\r\n超越范围,无法获取" );
}
}
public static ArrayList<Card> randomCard(int n) {
if (n > 54 || n < 0)
return null;
ArrayList<Card> rList = new ArrayList<>();
ArrayList<Card> cards = allCard();
Random r = new Random();
for (int i = 0; i < n; i++) {
int index = r.nextInt(cards.size());
Card rCard = cards.remove(index);//我认为全文最高明的地方在此(52张牌做到没有重复使用,且不用写那么多避免重复的代码),当remove掉一个时,返回值add到rList中,size随之减小
rList.add(rCard);
}
return rList;
}
public static ArrayList<Card> allCard() {
ArrayList<Card> allList = new ArrayList<>();
// 花色数组
String[] hs = {"黑桃", "红桃", "梅花", "方片"};
// 点数数组
String[] ds = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (int H = 0; H < hs.length; H++) {
for (int d = 0; d < ds.length; d++) {
Card card = new Card(hs[H], ds[d]);
// 添加到集合
allList.add(card);
}
}
return allList;
}
}
class Card {
private String ds; // 点数
private String hs; // 花色
public Card(String ds, String hs) {
this.ds = ds;
this.hs = hs;
}
public void showCard() {
System.out.print(ds + hs+" ");
}
}
原文:https://www.cnblogs.com/zx-coder/p/12798099.html