import java.util.ArrayList; import java.util.List; public class player { String name; int id; List<puke> pukes;//拿到的牌的列表 public player(String newname,int newid){ this.name=newname; this.id=newid; pukes=new ArrayList<puke>(); } } public class puke { public String name; public int h_id;//花色的ID,0-3,为了对比花的大小 public int s_id;//数的ID,为了对比数的大小,0-12 public puke(String newname, int newh_id, int news_id) { // TODO Auto-generated constructor stub this.name=newname; this.h_id=newh_id; this.s_id=news_id; } public puke(){ } } /* * 主方法 */ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Scanner; public class test { List<puke> pukelist=new ArrayList<puke>();//扑克牌列表 List<player> pList=new ArrayList<player>();//玩家列表 //创建扑克牌 public void createpukelist(){ String[] huastr={"方片","梅花","红桃","黑桃"}; String[] sustr={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; System.out.println("---------------创建扑克牌-----------------"); for(int i=0;i<=3;i++){ for(int j=0;j<13;j++){ puke puke=new puke(huastr[i]+sustr[j], i, j); pukelist.add(puke); } } System.out.println("--------------创建扑克牌成功---------------"); String pkString=""; for (puke pk1 : pukelist) { pkString=pkString+pk1.name+","; } System.out.println("扑克牌为:"+pkString); System.out.println("--------------开始洗牌---------------"); Collections.shuffle(pukelist);//对List随机排序 System.out.println("--------------洗牌结束---------------"); } //创建玩家 public void createplayer(){ Scanner scanner=new Scanner(System.in); int i=0; System.out.println("--------------创建玩家---------------"); while (i<2) { System.out.println("请输入玩家ID:"); if(scanner.hasNextInt()==true){ int playerid=scanner.nextInt(); System.out.println("请输入玩家姓名:"); String playername=scanner.next(); player pl=new player(playername, playerid); pList.add(pl); System.out.println("欢迎玩家:"+pl.name); i++; }else{ System.out.println("ID类型错误"); scanner=new Scanner(System.in);//如果不加该语句Scanner输入的值并不会刷新,所以就会出现无限死循环。 continue; } } } //发牌 public void putpuke(){ System.out.println("---------发牌开始-------------"); int k=0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { pList.get(j).pukes.add(pukelist.get(k)); System.out.println("玩家:"+pList.get(j).name+"拿牌"); k++; } } System.out.println(k); System.out.println("---------发牌结束-------------"); } public void contrastpuke(){ List<Integer> maxplids=new ArrayList<Integer>(); puke p1,p2,maxpuke=new puke(); p1=contrastPuke(pList.get(0).pukes.get(0),pList.get(0).pukes.get(1)); p2=contrastPuke(pList.get(1).pukes.get(0),pList.get(1).pukes.get(1)); System.out.println("玩家"+pList.get(0).name+"最大手牌为:"+p1.name); System.out.println("玩家"+pList.get(1).name+"最大手牌为:"+p2.name); maxpuke=contrastPuke(p1,p2); if(maxpuke==p1){ System.out.println("玩家"+pList.get(0).name+"胜利"); }else{ System.out.println("玩家"+pList.get(1).name+"胜利"); } for (player player : pList) { System.out.println("玩家"+player.name+"的牌为:"); for(int i=0;i<player.pukes.size();i++){ System.out.println(player.pukes.get(i).name); } } } //比较两个牌的大小,先按照牌的数进行比较,如果数一样,则比较花色大小 public puke contrastPuke(puke pk1,puke pk2){ if (pk1.s_id>pk2.s_id) { return pk1; }else if (pk1.s_id==pk2.s_id) { if(pk1.h_id>pk2.h_id){ return pk1; }else{ return pk2; } }else { return pk2; } } public static void main(String[] args) { // TODO Auto-generated method stub test test=new test(); test.createpukelist();//创建扑克牌 test.createplayer();//创建玩家列表 test.putpuke();//发牌 test.contrastpuke();//对比 } }
非常感谢,try catch死循环郁闷了两天,你的一句注释让我明白了原因。