1,创建对象
package anlidoudizhu;public class pai implements Comparable<pai> {private Double value;private String name;@Overridepublic int compareTo(pai o) {return -Double.compare(this.value,o.value);}//升序排序,treeset集合public Double getValue() {return value;}public void setValue(Double value) {this.value = value;}public String getName() {return name;}public void setName(String name) {this.name = name;}public pai() {}public pai(Double value, String name) {this.value = value;this.name = name;}
}
2,添加对象
public void add(){set.add(new pai(3.1,"红桃3"));set.add(new pai(4.1,"红桃4"));set.add(new pai(5.1,"红桃5"));set.add(new pai(6.1,"红桃6"));set.add(new pai(7.1,"红桃7"));set.add(new pai(8.1,"红桃8"));set.add(new pai(9.1,"红桃9"));set.add(new pai(10.1,"红桃10"));set.add(new pai(11.1,"红桃J"));set.add(new pai(12.1,"红桃Q"));set.add(new pai(13.1,"红桃K"));set.add(new pai(14.1,"红桃A"));set.add(new pai(15.1,"红桃2"));set.add(new pai(3.2,"黑桃3"));set.add(new pai(4.2,"黑桃4"));set.add(new pai(5.2,"黑桃5"));set.add(new pai(6.2,"黑桃6"));set.add(new pai(7.2,"黑桃7"));set.add(new pai(8.2,"黑桃8"));set.add(new pai(9.2,"黑桃9"));set.add(new pai(10.2,"黑桃10"));set.add(new pai(11.2,"黑桃J"));set.add(new pai(12.2,"黑桃Q"));set.add(new pai(13.2,"黑桃K"));set.add(new pai(14.2,"黑桃A"));set.add(new pai(15.2,"黑桃2"));set.add(new pai(3.3,"方块3"));set.add(new pai(4.3,"方块4"));set.add(new pai(5.3,"方块5"));set.add(new pai(6.3,"方块6"));set.add(new pai(7.3,"方块7"));set.add(new pai(8.3,"方块8"));set.add(new pai(9.3,"方块9"));set.add(new pai(10.3,"方块10"));set.add(new pai(11.3,"方块J"));set.add(new pai(12.3,"方块Q"));set.add(new pai(13.3,"方块K"));set.add(new pai(14.3,"方块A"));set.add(new pai(15.3,"方块2"));set.add(new pai(3.4,"梅花3"));set.add(new pai(4.4,"梅花4"));set.add(new pai(5.4,"梅花5"));set.add(new pai(6.4,"梅花6"));set.add(new pai(7.4,"梅花7"));set.add(new pai(8.4,"梅花8"));set.add(new pai(9.4,"梅花9"));set.add(new pai(10.4,"梅花10"));set.add(new pai(11.4,"梅花J"));set.add(new pai(12.4,"梅花Q"));set.add(new pai(13.4,"梅花K"));set.add(new pai(14.4,"梅花A"));set.add(new pai(15.4,"梅花2"));set.add(new pai(1999.1,"大王"));set.add(new pai(1733.1,"小王"));}
3,洗牌,发牌,判断地主
Random r=new Random();int rom=r.nextInt(0,51);//判断地主public void show(){for (int i = 0; i < set.size(); i++) {if(i<=16){set1.add((pai)set.toArray()[i]);}else if(i<=33) set2.add((pai)set.toArray()[i]);else if(i<=50) set3.add((pai)set.toArray()[i]);}if(rom<=16){set1.add((pai)set.toArray()[52]);set1.add((pai)set.toArray()[53]);set1.add((pai)set.toArray()[51]);System.out.println("1是地主");}else if(rom<=33){set2.add((pai)set.toArray()[52]);set2.add((pai)set.toArray()[53]);set2.add((pai)set.toArray()[51]);System.out.println("2是地主");}else if(rom<=50){set3.add((pai)set.toArray()[52]);set3.add((pai)set.toArray()[53]);set3.add((pai)set.toArray()[51]);System.out.println("3是地主");}for (pai pai : set1) {System.out.print(pai.getName()+" ");}System.out.println("\n");for (pai pai : set2) {System.out.print(pai.getName()+" ");}System.out.println("\n");for (pai pai : set3) {System.out.print(pai.getName()+" ");}System.out.println("\n");}
4,系统界面
public void xitong(){boolean flag = true;System.out.println("----------欢迎来到欢乐斗地主---------");System.out.println("----------洗牌请选择1---------");System.out.println("----------发牌请选择2---------");System.out.println("----------退出请选这3---------");Scanner sc=new Scanner(System.in);while(flag){System.out.println("-----------请请输入选项--------");int choose=sc.nextInt();switch (choose){case 1:add();break;case 2:show();break;case 3:flag=false;break;default:System.out.println("输入有误,请重新输入");}}}
5全部代码
package anlidoudizhu;import java.util.Scanner;public class tset {public static void main(String[] args) {manager m=new manager();m.xitong();}
}
package anlidoudizhu;import java.util.*;public class manager {Set<pai> set =new HashSet<>();Set<pai> set1 =new TreeSet<>();Set<pai> set2=new TreeSet<>();Set<pai> set3=new TreeSet<>();public void xitong(){boolean flag = true;System.out.println("----------欢迎来到欢乐斗地主---------");System.out.println("----------洗牌请选择1---------");System.out.println("----------发牌请选择2---------");System.out.println("----------退出请选这3---------");Scanner sc=new Scanner(System.in);while(flag){System.out.println("-----------请请输入选项--------");int choose=sc.nextInt();switch (choose){case 1:add();break;case 2:show();break;case 3:flag=false;break;default:System.out.println("输入有误,请重新输入");}}}public void add(){set.add(new pai(3.1,"红桃3"));set.add(new pai(4.1,"红桃4"));set.add(new pai(5.1,"红桃5"));set.add(new pai(6.1,"红桃6"));set.add(new pai(7.1,"红桃7"));set.add(new pai(8.1,"红桃8"));set.add(new pai(9.1,"红桃9"));set.add(new pai(10.1,"红桃10"));set.add(new pai(11.1,"红桃J"));set.add(new pai(12.1,"红桃Q"));set.add(new pai(13.1,"红桃K"));set.add(new pai(14.1,"红桃A"));set.add(new pai(15.1,"红桃2"));set.add(new pai(3.2,"黑桃3"));set.add(new pai(4.2,"黑桃4"));set.add(new pai(5.2,"黑桃5"));set.add(new pai(6.2,"黑桃6"));set.add(new pai(7.2,"黑桃7"));set.add(new pai(8.2,"黑桃8"));set.add(new pai(9.2,"黑桃9"));set.add(new pai(10.2,"黑桃10"));set.add(new pai(11.2,"黑桃J"));set.add(new pai(12.2,"黑桃Q"));set.add(new pai(13.2,"黑桃K"));set.add(new pai(14.2,"黑桃A"));set.add(new pai(15.2,"黑桃2"));set.add(new pai(3.3,"方块3"));set.add(new pai(4.3,"方块4"));set.add(new pai(5.3,"方块5"));set.add(new pai(6.3,"方块6"));set.add(new pai(7.3,"方块7"));set.add(new pai(8.3,"方块8"));set.add(new pai(9.3,"方块9"));set.add(new pai(10.3,"方块10"));set.add(new pai(11.3,"方块J"));set.add(new pai(12.3,"方块Q"));set.add(new pai(13.3,"方块K"));set.add(new pai(14.3,"方块A"));set.add(new pai(15.3,"方块2"));set.add(new pai(3.4,"梅花3"));set.add(new pai(4.4,"梅花4"));set.add(new pai(5.4,"梅花5"));set.add(new pai(6.4,"梅花6"));set.add(new pai(7.4,"梅花7"));set.add(new pai(8.4,"梅花8"));set.add(new pai(9.4,"梅花9"));set.add(new pai(10.4,"梅花10"));set.add(new pai(11.4,"梅花J"));set.add(new pai(12.4,"梅花Q"));set.add(new pai(13.4,"梅花K"));set.add(new pai(14.4,"梅花A"));set.add(new pai(15.4,"梅花2"));set.add(new pai(1999.1,"大王"));set.add(new pai(1733.1,"小王"));}Random r=new Random();int rom=r.nextInt(0,51);//判断地主public void show(){for (int i = 0; i < set.size(); i++) {if(i<=16){set1.add((pai)set.toArray()[i]);}else if(i<=33) set2.add((pai)set.toArray()[i]);else if(i<=50) set3.add((pai)set.toArray()[i]);}if(rom<=16){set1.add((pai)set.toArray()[52]);set1.add((pai)set.toArray()[53]);set1.add((pai)set.toArray()[51]);System.out.println("1是地主");}else if(rom<=33){set2.add((pai)set.toArray()[52]);set2.add((pai)set.toArray()[53]);set2.add((pai)set.toArray()[51]);System.out.println("2是地主");}else if(rom<=50){set3.add((pai)set.toArray()[52]);set3.add((pai)set.toArray()[53]);set3.add((pai)set.toArray()[51]);System.out.println("3是地主");}for (pai pai : set1) {System.out.print(pai.getName()+" ");}System.out.println("\n");for (pai pai : set2) {System.out.print(pai.getName()+" ");}System.out.println("\n");for (pai pai : set3) {System.out.print(pai.getName()+" ");}System.out.println("\n");}}
package anlidoudizhu;public class pai implements Comparable<pai> {private Double value;private String name;@Overridepublic int compareTo(pai o) {return -Double.compare(this.value,o.value);}//升序排序,treeset集合public Double getValue() {return value;}public void setValue(Double value) {this.value = value;}public String getName() {return name;}public void setName(String name) {this.name = name;}public pai() {}public pai(Double value, String name) {this.value = value;this.name = name;}
}
6,遇到的问题与解决办法
1,hashset和treeset不能直接通过索引获取元素
需要使用toarray()方法转换成数组,在使用(pai)进行强制类型转换
2,print()方法打印不换行
3,else if 而不是if
4,treeset集合,自定义排序,在类中需要接接口Comparable小数得比较,用Double方法,this是左边的值,o是右边的值,要是升序左边-右边大于0,反之小于0