展开全部
简单实现,可供参考,没考虑输入异常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; // 其他
}
}