java扑克牌比大小_JAVA编程判断扑克牌大小

展开全部

简单实现,可供参考,没考虑输入异常public class Main {

static Map numMap = new HashMap<>();

static {

numMap.put("2", 2);

numMap.put("3", 3);

numMap.put("4", 4);

numMap.put("5", 5);

numMap.put("6", 6);

numMap.put("7", 7);

numMap.put("8", 8);

numMap.put("9", 9);

numMap.put("10", 10);

numMap.put("J", 11);

numMap.put("Q", 12);

numMap.put("K", 13);

numMap.put("A", 14);

}

public static void main(String[] args) {

int[] nums = new int[5];  // 记录牌的大小32313133353236313431303231363533e59b9ee7ad9431333365653264

String[] colors = new String[5];  // 记录牌的花色

// 输入5张牌

Scanner in = new Scanner(System.in);

for (int i = 0; i 

String[] split = in.nextLine().split(" ");

// 用数字代替 J、Q、K、A 以方便计算

nums[i] = numMap.get(split[0].toUpperCase());

colors[i] = split[1].toUpperCase();

}

int result = parse(nums, colors);

System.out.println(result);

}

private static int parse(int[] nums, String[] colors) {

Map numCount = new HashMap<>(); //统计每种牌(大小)的数量

for (Integer num : nums) {

Integer n = numCount.get(num);

numCount.put(num, n == null ? 1 : n + 1);

}

Map colorCount = new HashMap<>();  // 统计每种花色的数量

for (String color : colors) {

Integer c = colorCount.get(color);

colorCount.put(color, c == null ? 1 : c + 1);

}

// 对牌的大小进行排序

for (int i = 0; i 

for (int j = i + 1; j 

if (nums[i] > nums[j]) {

int tmp = nums[i];

nums[i] = nums[j];

nums[j] = tmp;

}

}

}

boolean isFlush = colorCount.size() == 1; // 是否是同花, 只有1种花色则是同花

boolean isStraight = true; // 是否是顺子

for (int i = 1; i 

// 如果后一张牌不是刚好比前一张大一则不是顺子

if (nums[i] - 1 != nums[i - 1]) {

isStraight = false;

break;

}

}

// 判断是否是同花顺

if (isFlush && isStraight) {

return 1; //同花顺

}

// 判断是否是四条或葫芦

if (numCount.size() == 2) {  // 只有两种牌则肯定是四条或者葫芦

for (Integer count : numCount.values()) {

if (count == 1 || count == 4) {

return 2; // 四条

}

if (count == 2 || count == 3) {

return 3; // 葫芦

}

}

}

// 判断是否是同花

if (isFlush) {

return 4; // 同花

}

// 判断是否是顺子

if (isStraight) {

return 5; // 顺子

}

// 判断是否是三条

for (Integer count : numCount.values()) {

if (count == 3) {

return 6; // 三条

}

}

return 7; // 其他

}

}

99cbd488e8ec6ae35cbe864433056537.png    ca2e006e04a9d27e4ae8c19b776e154a.png          59735680b1c3e2b330efd253f33be816.png     bc4f6f3b66649475fbabf8a53da9e394.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值