集合框架练习8
package dapaiba; import java.util.Arrays; public class puker implements Comparable<puker>{ int style; int value; private String[]STYLE={"方块","梅花","红心","黑桃"}; private String[]VALUE={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; public puker(int style, int value) { super(); this.style = style; this.value = value; } @Override public String toString() { return STYLE[style]+VALUE[value]; } public int getStyle() { return style; } public int getValue() { return value; } @Override public int compareTo(puker o) { if(this.style==o.style){ return this.value-o.value; } return this.style-o.style; } }
package dapaiba; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class pukerbox{ private List<puker>list=new ArrayList<puker>(); //构造方法,用来初始化52张扑克 public pukerbox(){ for(int i=0;i<52;i++){ list.add(new puker(i%4,i/4)); } } //洗牌并输出 public void shuffle(){ Collections.shuffle(list); this.show(); } //排序 public void sort(){ Collections.sort(list); this.show(); } //排序2 public void sortbyvalue(){ Collections.sort(list,new Comparator<puker>(){ @Override public int compare(puker o1, puker o2) { return (o1.value*4+o1.style)-(o2.value*4+o2.style); }}); this.show(); } //输出纸牌 public void show(){ for(puker p:list){ System.out.println(p); } } puker p1=new puker(0, 1); puker p2=new puker(3, 1); public void big(){ if(p1.value>p2.value){ System.out.println(p1); } if(p1.value<p2.value){ System.out.println(p2); } if(p1.value==p2.value){ if(p1.style<p2.style){ System.out.println(p2); } } } }
package dapaiba; public class TestPuker { public static void main(String[] args) { pukerbox box=new pukerbox(); //box.show(); box.big(); } }