写了一下午,代码上传了,请大家指点一二
个人写的时候有个问题一直没法解决,请大家指点下 Scannaer的那个。读取字符串老是读取到 输入法里的拼音 。而读取非拼音的字符就正常,写汉字时他的光标不会自动跳下去。很是费解。
package Puke1; //绝世高手的对决 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Test { public List<Puke> cpuke; // 全牌的集合 public List<player> players; // 玩家集合 public Test() { this.cpuke = new ArrayList<Puke>(); this.players = new ArrayList<player>(); } public static void main(String[] args) { Test t = new Test(); t.cpoke(); // 创建扑克 t.xpuke(); // 洗牌 t.cJwan(); // 创建玩家 t.fPai(); // 发牌和比较牌 } // 创建牌 public void cpoke() { System.out.println("----------创建扑克中---------"); for (int i = 0; i < 4; i++) { for (int m = 0; m < 14; m++) { Puke p = new Puke(i, m + 1); cpuke.add(p); } } for (int i = 0; i < cpuke.size(); i++) { System.out.print("[" + cpuke.get(i).huase + cpuke.get(i).num + "]"); } System.out.println("\n" + "----------创建完毕----------"); } // 洗牌 public void xpuke() { System.out.println("----------开始洗牌----------"); Collections.shuffle(cpuke); for (int i = 0; i < cpuke.size(); i++) { // System.out.print("[" + cpuke.get(i).huase + cpuke.get(i).num + // "]"); //洗牌后的牌打印 } System.out.println("\n"); } // 创建玩家 public void cJwan() { while (true) { try { System.out.println("请输入第一位玩家的ID:"); Scanner input = new Scanner(System.in); int t1 = input.nextInt(); System.out.println("请输入第一位玩家的姓名:"); String t2 = input.next(); player p = new player(t1, t2); players.add(p); break; } catch (Exception e) { System.out.println("请输入整形数据类型的ID:"); } } while (true) { try { Scanner input = new Scanner(System.in); System.out.println("请输入第二位玩家的ID:"); int t1 = input.nextInt(); System.out.println("请输入第二位玩家的姓名:"); String t2 = input.next(); player p = new player(t1, t2); players.add(p); break; } catch (Exception e) { System.out.println("请输入整形数据类型的ID:"); } } System.out.println("欢迎玩家:" + players.get(0).name); System.out.println("欢迎玩家:" + players.get(1).name); } // 发牌且比较 public void fPai() { players.get(0).sPai.add(cpuke.get(0)); players.get(1).sPai.add(cpuke.get(1)); players.get(0).sPai.add(cpuke.get(2)); players.get(1).sPai.add(cpuke.get(3)); System.out.println("发牌结束~"); System.out.println("开始游戏:"); int a = 0; int b = 0; if (players.get(0).sPai.get(0).numy > players.get(0).sPai.get(1).numy) { System.out.println("玩家:" + players.get(0).name + "最大的手牌为;" + players.get(0).sPai.get(0).huase + players.get(0).sPai.get(0).num); } else { System.out.println("玩家:" + players.get(0).name + "最大的手牌为;" + players.get(0).sPai.get(1).huase + players.get(0).sPai.get(1).num); a = 1; } if (players.get(1).sPai.get(0).numy > players.get(1).sPai.get(1).numy) { System.out.println("玩家:" + players.get(1).name + "最大的手牌为;" + players.get(1).sPai.get(0).huase + players.get(1).sPai.get(0).num); } else { System.out.println("玩家:" + players.get(1).name + "最大的手牌为;" + players.get(1).sPai.get(1).huase + players.get(1).sPai.get(1).num); b = 1; } if (players.get(0).sPai.get(a).numy > players.get(1).sPai.get(b).numy) { System.out.println(players.get(0).name + "获胜,你可真牛b啊"); } else if (players.get(0).sPai.get(a).numy == players.get(1).sPai .get(b).numy) { if (players.get(0).sPai.get(a).huaseint > players.get(1).sPai .get(b).huaseint) { System.out.println(players.get(0).name + "获胜,你可真牛b啊"); } } else { System.out.println(players.get(1).name + "获胜,你可真牛b啊"); } System.out.println("玩家各自的手牌:"); System.out.print(players.get(0).name + ":" + players.get(0).sPai.get(0).huase + players.get(0).sPai.get(0).num); System.out.println(players.get(0).sPai.get(1).huase + players.get(0).sPai.get(1).num); System.out.print(players.get(1).name + "." + players.get(1).sPai.get(0).huase + players.get(1).sPai.get(0).num); System.out.println(players.get(1).sPai.get(1).huase + players.get(1).sPai.get(1).num); } }
package Puke1; //扑克类 public class Puke { public String huase; public String num; public int numy; public int huaseint; public Puke(int a,int b){ this.numy=b; this.huaseint=huaseint; switch(a){ case 0:this.huase="红桃"; break; case 1:this.huase="方片"; break; case 2:this.huase="梅花"; break; case 3:this.huase="黑桃"; break; } switch(b){ case 11:this.num="J"; break; case 12:this.num="Q"; break; case 13:this.num="K"; break; case 14:this.num="A"; break; default: this.num=Integer.toString(b); } } }
package Puke1; //玩家类 import java.util.ArrayList; import java.util.List; public class player { public int id; public String name; public List <Puke>sPai; public player(int id,String name){ this.id=id; this.name=name; this.sPai=new ArrayList <Puke>(); } }
比较方法做的不是很好。求更好的方法。