java 扑克牌排序_java 怎么写compareto方法实现扑克牌排序

展开全部

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"; //黑桃e69da5e887aa62616964757a686964616f31333337383265

public static final String RED = "2"; //红桃

public static final String CLUB = "1"; //梅花

public static final String DIAMOND = "0";//方块

}

------------------------------------------------------------------------------------

public class OrderTool {

public List doOrder(Card[] card_array) {

List list = new ArrayList();

// 按类排序

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 list = new OrderTool().doOrder(c);

for(Card card : list){

System.out.println("花色:"+OrderTool.faceTranslater(Integer.parseInt(card.getSuit()))+"&"+"牌面:"+card.getFace());

}

}

本回答被网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值