写的太简陋了。有空再优化下
main
package poke2; import java.util.Collections; import java.util.List; public class main { public static void main(String[] args) { pukepai p1=new pukepai(); p1.show(); List<Poke> p=p1.xipai(); /*System.out.println(); p1.shownewlist(p);*/ player player1=new player(); player player2=new player(); Poke p3=p.get(0); Poke p4=p.get(1); Poke p5=p.get(2); Poke p6=p.get(3); player1.shoupai.add(p3); player2.shoupai.add(p4); player1.shoupai.add(p5); player2.shoupai.add(p6); for (Poke x:player1.shoupai) { System.out.println(x.huase+x.shuzi); } System.out.println("-------------"); for (Poke x:player2.shoupai) { System.out.println(x.huase+x.shuzi); } Collections.sort(player1.shoupai); Collections.sort(player2.shoupai); Poke poke01=player1.shoupai.get(1); Poke poke02=player2.shoupai.get(1); if(poke01.value>poke02.value){ System.out.println("楼上赢了"); }else{ System.out.println("楼下赢了"); } } }
2.player
package poke2; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; public class player { public String name; public int id; public List<Poke> shoupai; public Scanner console; public player(){ shoupai=new ArrayList<Poke>(); System.out.println("请输入玩家姓名"); console=new Scanner(System.in); String name=console.next(); System.out.println("玩家:"+name); while(true) { try { System.out.println("请输入玩家ID"); console=new Scanner(System.in); int id = console.nextInt(); System.out.println("玩家ID为"+id); break; } catch (InputMismatchException e) { System.out.println("输入的id不是int类型,请重新输入"); }catch (Exception e){ System.out.println("其他问题"); } } } }
3Poke
package poke2; public class Poke implements Comparable<Poke> { String huase; String shuzi; double value; double huasevalue; public Poke(String huase,String shuzi,double value ,double huasevalue){ this.huase=huase; this.shuzi=shuzi; this.value=value+huasevalue; } @Override public int compareTo(Poke o) { if(this.value>o.value){ return 1; }else if(this.value<o.value){ return -1; }else{ return 0; } } }
4pukepa
package poke2; import java.util.ArrayList; import java.util.List; import java.util.Random; public class pukepai { List<Poke> list1; String[] class0={"黑桃", "红桃", "梅花", "方片"}; String[] no={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; int[]vv={2,3,4,5,6,7,8,9,10,11,12,13,14}; double[] huaseva={0.4,0.3,0.2,0.1}; //Poke p1=new Poke("黑桃","2",2); public pukepai(){ list1=new ArrayList(); for(int i=0;i<4;i++){ for(int j=0;j<13;j++){ Poke poke0=new Poke(class0[i],no[j],vv[j],huaseva[i]); list1.add(poke0); } } } public void show(){ for (Poke p:list1 ) { System.out.print(p.huase+p.shuzi+" "); } } public List<Poke> xipai() { List<Poke> list2; { list2 = new ArrayList<Poke>(); } for (int i = 0; i < 52; i++) { Random random = new Random(); Poke w; do { int q = random.nextInt(52); w = list1.get(q); } while (list2.contains(w)); list2.add(w); } return list2; } public void shownewlist(List<Poke> p0){ for (Poke p:p0 ) { System.out.print(p.huase+p.shuzi+" "); } } }