1)定义一个Poker类,类的成员变量用于保存扑克牌的花色和点数。 (2)给Poker类创建不同的构造器(包括无参数构造器和有参数构造器); (3)给Poker类提供一个方法用于打印输出一张牌信息(包括花色和点数). (4)另外在定义个公共类PokerApp,包括一个成员变量(Pocker数组),用来保存52张牌。 (5)在公共类PokerApp中定义三个方法,一个方法用来创建52张牌,并保存在成员变量(Pocker数组)中。第二个方法,用来模拟洗牌;第三个方法把洗好的牌显示打印。 (6)在公共类PokerApp中使用main方法那测试创建牌、洗牌和打印牌等操作。
时间: 2023-06-23 12:09:37 浏览: 174
2. 定义一个Card类,表示扑克牌,该类包括下述成员
1. 定义Poker类
```java
public class Poker {
private String suit; //花色
private String rank; //点数
public Poker(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public Poker() {
this.suit = "";
this.rank = "";
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public void print() {
System.out.println("花色:" + suit + ",点数:" + rank);
}
}
```
2. 在公共类PokerApp中定义一个Poker数组,用来保存52张牌,并创建不同的构造器
```java
public class PokerApp {
private Poker[] pokers;
public PokerApp() {
pokers = new Poker[52]; //初始化牌组
String[] suits = {"♠", "♥", "♣", "♦"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int index = 0;
for (String suit : suits) {
for (String rank : ranks) {
pokers[index] = new Poker(suit, rank);
index++;
}
}
}
public PokerApp(int num) {
pokers = new Poker[num];
}
public PokerApp(Poker[] pokers) {
this.pokers = pokers;
}
// 洗牌
public void shuffle() {
Random rand = new Random();
for (int i = pokers.length - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
Poker temp = pokers[i];
pokers[i] = pokers[j];
pokers[j] = temp;
}
}
// 打印牌组
public void printPokers() {
for (Poker poker : pokers) {
poker.print();
}
}
public static void main(String[] args) {
PokerApp pokerApp = new PokerApp();
System.out.println("原始牌组:");
pokerApp.printPokers();
System.out.println("洗牌后的牌组:");
pokerApp.shuffle();
pokerApp.printPokers();
}
}
```
阅读全文