java 怎么写compareto方法实现扑克牌排序 5
在Card类中,变量face牌面,字符串类型;suit花色,字符串类型。按AKQJ1098765432的顺序比较,相同的按花色(黑桃,红桃,梅花,方块)...
在Card类中,变量face牌面,字符串类型;suit花色,字符串类型。按A K Q J 10 9 8 7 6 5 4 3 2 的顺序比较,相同的按花色(黑桃,红桃,梅花,方块)
展开
2个回答
2015-12-07 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 创建一个ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是编号)
Collections.shuffle(array);
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 写看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
展开全部
public class Card {
private String face;
private String suit;
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public Card(String face, String suit) {
super();
this.face = face;
this.suit = suit;
}
public Card() {
// TODO Auto-generated constructor stub
}
----------------------------------------------------------------------------------
public class Consts {
public static final String BLACK = "3"; //黑桃
public static final String RED = "2"; //红桃
public static final String CLUB = "1"; //梅花
public static final String DIAMOND = "0";//方块
}
------------------------------------------------------------------------------------
public class OrderTool {
public List<Card> doOrder(Card[] card_array) {
List<Card> list = new ArrayList<Card>();
// 按类排序
if (card_array != null && card_array.length > 0) {
Card temp = null;
int flag = 0;
for (int i = 0; i < card_array.length; i++) {
if (flag > 0) {
System.out.println("请检查传入的纸牌值!");
break;
}
for (int j = i + 1; j < card_array.length; j++) {
int i_suit = this.convert(card_array[i].getFace());
int j_suit = this.convert(card_array[j].getFace());
if (i_suit == -1 || j_suit == -1) {
flag++;
break;
}
if (i_suit > j_suit) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
} else if (i_suit == j_suit) {
int i_face = Integer.parseInt(card_array[i].getSuit());
int j_face = Integer.parseInt(card_array[j].getSuit());
if (i_face < j_face) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
}
}
}
}
for(Card card : card_array){
list.add(card);
}
}
return list;
}
private int convert(String param) {
int res = 0;
try {
res = Integer.parseInt(param);
return res;
} catch (Exception e) {
if ("A".equalsIgnoreCase(param)) {
return 1;
} else if ("J".equalsIgnoreCase(param)) {
return 11;
} else if ("Q".equalsIgnoreCase(param)) {
return 12;
} else if ("K".equalsIgnoreCase(param)) {
return 13;
} else {
System.out.println("拿的是王吧!");
return -1;
}
}
}
public static String faceTranslater(int face){
String res = "";
switch(face){
case 3:res = "黑桃";break;
case 2:res = "红桃";break;
case 1:res = "梅花";break;
case 0:res = "方块";break;
}
return res;
}
//测试
public static void main(String[] args) {
Card c0 = new Card("A",Consts.BLACK);
Card c1 = new Card("2",Consts.BLACK);
Card c2 = new Card("A",Consts.RED);
Card c3 = new Card("5",Consts.CLUB);
Card c4 = new Card("4",Consts.RED);
Card c5 = new Card("3",Consts.DIAMOND);
Card c6 = new Card("4",Consts.BLACK);
Card[] c = {c0,c1,c2,c3,c4,c5,c6};
List<Card> list = new OrderTool().doOrder(c);
for(Card card : list){
System.out.println("花色:"+OrderTool.faceTranslater(Integer.parseInt(card.getSuit()))+"&"+"牌面:"+card.getFace());
}
}
private String face;
private String suit;
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public Card(String face, String suit) {
super();
this.face = face;
this.suit = suit;
}
public Card() {
// TODO Auto-generated constructor stub
}
----------------------------------------------------------------------------------
public class Consts {
public static final String BLACK = "3"; //黑桃
public static final String RED = "2"; //红桃
public static final String CLUB = "1"; //梅花
public static final String DIAMOND = "0";//方块
}
------------------------------------------------------------------------------------
public class OrderTool {
public List<Card> doOrder(Card[] card_array) {
List<Card> list = new ArrayList<Card>();
// 按类排序
if (card_array != null && card_array.length > 0) {
Card temp = null;
int flag = 0;
for (int i = 0; i < card_array.length; i++) {
if (flag > 0) {
System.out.println("请检查传入的纸牌值!");
break;
}
for (int j = i + 1; j < card_array.length; j++) {
int i_suit = this.convert(card_array[i].getFace());
int j_suit = this.convert(card_array[j].getFace());
if (i_suit == -1 || j_suit == -1) {
flag++;
break;
}
if (i_suit > j_suit) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
} else if (i_suit == j_suit) {
int i_face = Integer.parseInt(card_array[i].getSuit());
int j_face = Integer.parseInt(card_array[j].getSuit());
if (i_face < j_face) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
}
}
}
}
for(Card card : card_array){
list.add(card);
}
}
return list;
}
private int convert(String param) {
int res = 0;
try {
res = Integer.parseInt(param);
return res;
} catch (Exception e) {
if ("A".equalsIgnoreCase(param)) {
return 1;
} else if ("J".equalsIgnoreCase(param)) {
return 11;
} else if ("Q".equalsIgnoreCase(param)) {
return 12;
} else if ("K".equalsIgnoreCase(param)) {
return 13;
} else {
System.out.println("拿的是王吧!");
return -1;
}
}
}
public static String faceTranslater(int face){
String res = "";
switch(face){
case 3:res = "黑桃";break;
case 2:res = "红桃";break;
case 1:res = "梅花";break;
case 0:res = "方块";break;
}
return res;
}
//测试
public static void main(String[] args) {
Card c0 = new Card("A",Consts.BLACK);
Card c1 = new Card("2",Consts.BLACK);
Card c2 = new Card("A",Consts.RED);
Card c3 = new Card("5",Consts.CLUB);
Card c4 = new Card("4",Consts.RED);
Card c5 = new Card("3",Consts.DIAMOND);
Card c6 = new Card("4",Consts.BLACK);
Card[] c = {c0,c1,c2,c3,c4,c5,c6};
List<Card> list = new OrderTool().doOrder(c);
for(Card card : list){
System.out.println("花色:"+OrderTool.faceTranslater(Integer.parseInt(card.getSuit()))+"&"+"牌面:"+card.getFace());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询