package com.exam; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; /* * 1、扑克牌创建 * 2、进行洗牌 * 3、玩家创建 * 4、进行发牌 */ public class PTest { private List<Card> pokerList = new ArrayList<Card>();//创建扑克List private List<People1> peopleList = new ArrayList<People1>();//创建玩家List /* *1、 扑克牌创建 */ public void creatcard(){ String[] num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] type={"红桃","方块","梅花","黑桃"}; String[] other={"大王","小王"}; int sum=0;//统计总牌数量 int sum1=0;//每四个换一行数量 System.out.println("----创建扑克牌----"); for(int i=0;i<num.length;i++){ for(int j=0;j<type.length;j++){ pokerList.add(new Card(num[i],type[j],"")); } } for (int d=0;d<other.length;d++){ pokerList.add(new Card("","",other[d])); } for (Card poker : pokerList) { System.out.print(poker.getNum() + poker.getType()+" "+ poker.getOther()); sum++; sum1++; if(sum1%10==0){ System.out.println("\r\n"); } } System.out.println("\r\n"); System.out.println("扑克牌总数: "+ sum); System.out.println("----扑克牌已经创建成功----"); } /* * 2、洗牌 */ public void upsetPoker() { int sum1=0;//每四个换一行数量 System.out.println("------下面开始洗牌...-------"); Collections.shuffle(pokerList); System.out.println("------洗牌结束!洗牌后是------"); //验证 //System.out.println("洗牌后为:"+pokerList.toString()); for (Card poker : pokerList) { System.out.print(poker.getNum() + poker.getType()+" "+ poker.getOther()+" "); sum1++; if(sum1%10==0){ System.out.println("\r\n"); } } } /* * 3、提示创建玩家 */ public void createPeople() { System.out.println("\r\n"+"------创建4个玩家------"); int peopleNum=4; Integer id; String name; Scanner console = null; for (int j = 1; j <= peopleNum; j++) { System.out.println("请输入第"+j+"位玩家的ID和姓名:"); while(true){ try { System.out.println("输入ID:"); console = new Scanner(System.in); id = console.nextInt(); if(peopleList.contains(new People(id,null))) { System.out.println("此Id已存在!"); continue; } break; }catch (InputMismatchException e) { System.out.println("请输入整数类型ID!"); continue; } } while(true){ System.out.println("输入姓名:"); console = new Scanner(System.in); name = console.nextLine(); if(peopleList.contains(new People(null,name))) { System.out.println("此name已存在!"); continue; } break; } peopleList.add(new People1(id,name)); } console.close();//关闭输入 //欢迎 for (People1 people : peopleList) { System.out.println("----欢迎玩家:"+people.getName()); } } /* * 5、提示发牌---发牌---展示--提示发牌结束 */ private void issuePoker() { System.out.println("------开始发牌...-------"); int d=0; int sum=0; if(54%4!=0){ d=1; } int i = (54/4)+1;//发牌次数 int j = 0;//初始牌下标 while(i>0) { for (People1 people : peopleList) { System.out.println("---玩家"+people.getName()+"-拿牌"); people.cards.add(pokerList.get(j)); j++; sum++; if(sum==54){ break; } } i--; } System.out.println("------发牌结束!-------"); System.out.println("玩家各自的手牌为:"); for (People1 people : peopleList) { System.out.println(people.getName()+":"+people.cards.toString()); }} public static void main(String[] args) { // TODO Auto-generated method stub PTest go = new PTest(); go.creatcard(); go.upsetPoker(); go.createPeople(); go.issuePoker(); System.out.println(""); } }
package com.exam; /* * Car类用来封装牌的类型、花色、大小王参数 */public class Card { private String num; private String type; private String other; public Card(String num,String type,String other){ this.num=num; this.type=type; this.other=other; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return num + type + other; } }
package com.exam; /* * People类用来封装人员id、name、手牌 */ import java.util.ArrayList; import java.util.List; public class People1 { private Integer id; private String name; public List<Card> cards; public People1 (Integer id, String name) { super(); this.id = id; this.name = name; this.cards = new ArrayList<Card>(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
小测试一枚,java基础看了好多遍,要泪崩了!!!!
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦