可根据两手牌的长度是否相同来将牌型分为两类:
长度不同:
- 其中一方是大小王,则直接输出大小王
- 其中一方是炸弹,则找到炸弹并输出
- 不是炸弹也不是大小王,无法比较
长度相同:
- 长度为1或3或4或5,直接比较各自的第一张牌的大小即可,找到较大的输出
- 长度为2,先判断其中一方是否为大小王,是则输出大小王,不是则比较首张牌,找到较大的输出
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("3", 3);
map.put("4", 4);
map.put("5", 5);
map.put("6", 6);
map.put("7", 7);
map.put("8", 8);
map.put("9", 9);
map.put("10", 10);
map.put("J", 11);
map.put("Q", 12);
map.put("K", 13);
map.put("A", 14);
map.put("2", 15);
map.put("joker", 16);
map.put("JOKER", 17);
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split("-");
String[] a = s[0].split(" ");
String[] b = s[1].split(" ");
if (a.length != b.length) { //长度不相等:1.大小王 2.炸弹 3.Error
if ("joker JOKER".equals(s[0]) || "joker JOKER".equals(s[1])) {
System.out.println("joker JOKER");
} else {
if (a.length == 4) {
System.out.println(s[0]);
} else if (b.length == 4) {
System.out.println(s[1]);
} else {
System.out.println("ERROR");
}
}
} else { //长度相等:1.长度为2时可能是大小王 2.长度为1345可直接比大小
if (a.length == 1 || a.length == 3 || a.length == 4 || a.length == 5) {
if (map.get(a[0]) > map.get(b[0])) {
System.out.println(s[0]);
} else {
System.out.println(s[1]);
}
} else if (a.length == 2) {
if ("joker JOKER".equals(s[0]) || "joker JOKER".equals(s[0])) {
System.out.println("joker JOKER");
} else if (map.get(a[0]) > map.get(b[0])) {
System.out.println(s[0]);
} else {
System.out.println(s[1]);
}
}
}
}
}

京公网安备 11010502036488号