求一个java的简单代码

2025-01-05 08:16:47
推荐回答(4个)
回答1:

Poker类,代码贴上

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Poker {
public static List pokerList(){
List list = new ArrayList();
list.add("大王");
list.add("小王");

String[] type = {"红桃","黑桃","梅花","方块"};
String[] value = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

for (String t : type) {
for (String v : value) {
list.add(t+v);
}
}

return list;
}
}

随即抽牌类,代码贴上

import java.util.List;
import java.util.Random;

public class RandomPoker {
public static String getRandomPoker(){
List pokerList = Poker.pokerList();

int pokerIndex = new Random().nextInt(pokerList.size());

String poker = pokerList.get(pokerIndex-1);

return poker;
}

//写个main方法测试一下
public static void main(String[] args) {
System.out.println(getRandomPoker());
}
}

回答2:

用继承啊,2个类是1:牌类,2:扑克牌类。扑克牌类继承牌类,再用random随机抽取一张。

回答3:

又不具体

回答4:

学习了,感谢三楼!