你好,请问我写的模拟斗地主程序输出的应该是按牌的大小排序的,为什么实际输出的是按花色排序的
package project2DouDiZhuUP;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
//项目二:模拟斗地主升级版
/*需求:通过程序实现斗地主过程中的洗牌,发牌,看牌操作。要求对每个人拿到的牌进行排序
思路:
1.创建HashMap,键是编号(0-53),值是牌
2.创建ArrayList,存储编号
3.创建花色数组和点数数组
4.从0开始往HashMap里存储编号,并存储对应的牌。同时往ArrayList里存储编号
5.洗牌(洗的是ArrayList里的编号)
6.发牌(发的是ArrayList里的编号,为了保证编号是排序的,使用TreeSet集合接收---即创建TreeSet对象)
7.定义看牌方法(遍历TreeSet集合,获取编号,映射到HashMap中对应的牌)并调用
*/
public class Demo {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();//1.创建HashMap,键是编号(0-53),值是牌
ArrayList<Integer> array = new ArrayList<Integer>();//2.创建ArrayList,存储编号
String[] colors = {"♥", "♠", "♣", "◇"};//3.创建花色数组和点数数组
String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
//4.从0开始往HashMap里存储编号,并存储对应的牌。同时往ArrayList里存储编号
int index = 0;
for (String color : colors) {
for (String number : numbers) {
hm.put(index, color + number);
array.add(index);
index++;
}
}
hm.put(index, "大王");
array.add(index);
index++;
hm.put(index, "小王");
array.add(index);
Collections.shuffle(array);//5.洗牌(洗的是ArrayList里的编号)
//6.发牌(发的是ArrayList里的编号,为了保证编号是排序的,使用TreeSet集合接收---即创建TreeSet对象)
TreeSet<Integer> lqxbhpoker = new TreeSet<Integer>();//玩家一:林青霞
TreeSet<Integer> fqybhpoker = new TreeSet<Integer>();//玩家二:风清扬
TreeSet<Integer> ldhbhpoker = new TreeSet<Integer>();//玩家三:刘德华
TreeSet<Integer> dpbhpoker = new TreeSet<Integer>();//底牌
for (int i = 0; i < array.size(); i++) {
int x = array.get(i);
if (i >= array.size() - 3) {
dpbhpoker.add(x);
} else if (i % 3 == 0) {
lqxbhpoker.add(x);
} else if (i % 3 == 1) {
fqybhpoker.add(x);
} else if (i % 3 == 2) {
ldhbhpoker.add(x);
}
}
lookpoker("林青霞", lqxbhpoker, hm);
lookpoker("风清扬", fqybhpoker, hm);
lookpoker("刘德华", ldhbhpoker, hm);
lookpoker("底牌", dpbhpoker, hm);
}
//7.定义看牌方法(遍历TreeSet集合,获取编号,映射到HashMap中对应的牌)并调用
public static void lookpoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String poker = hm.get(key);
System.out.print(poker + " ");
}
System.out.println();
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/f35c0c07fe884911a511904bc91eb082.png "#left")