实体类 package com.itheima.d3_collection_test; public class Card { private String number; // 点数 private String color; // 花色 private int size; // 大小。 public Card() { } public Card(String number, String color, int size) { this.number = number; this.color = color; this.size = size; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } @Override public String toString() { return number + color; } }
房间类
package com.itheima.d3_collection_test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; // 房间类。 public class Room { // 主要要有一副牌:54张。 private ListallCards = new ArrayList<>(); // [] public Room(){ // 1、做牌 // 把点数拿到程序中来: 类型确定,个数确定,请用数组。 String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; // 把花色拿到程序中来:类型确定,个数确定,请用数组。 String[] colors = {"♠", "♥", "♣", "♦"}; // 2、遍历点数,再遍历花色 int size = 0; for (String number : numbers) { size++; for (String color : colors) { // 3、创建一个牌对象封装一张牌数据。 Card c = new Card(number, color, size); allCards.add(c); // 加入到集合中去。 } } allCards.add(new Card("", " 标签:String,斗地主,color,案例,Card,前期,public,allCards,size From: https://www.cnblogs.com/sisiyibailiu/p/16969730.html