题解 | #扑克牌大小#

扑克牌大小

https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str;
        String[] arr;
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            str = in.nextLine();
            arr = str.split("-");
            if (arr[0].equals("joker JOKER") || arr[1].equals("joker JOKER")) {
                System.out.println("joker JOKER");
                break;
            } else if (arr[0].split(" ").length == 4 && arr[1].split(" ").length == 4) {
                if (compare(arr[0].split(" ")[0], arr[1].split(" ")[0])) {
                    System.out.println(arr[0]);
                    break;
                }else{
                    System.out.println(arr[1]);
                }

            }else if(arr[0].split(" ").length == 4){
                System.out.println(arr[0]);
            }else if(arr[1].split(" ").length == 4){
                System.out.println(arr[1]);
            }else if(arr[0].split(" ").length == 3 && arr[1].split(" ").length == 3){
                if(compare(arr[0].split(" ")[0], arr[1].split(" ")[0])){
                    System.out.println(arr[0]);
                    break;
                }else{
                    System.out.println(arr[1]);
                }
            }else if(arr[0].split(" ").length == 5 && arr[1].split(" ").length == 5){
                if(compare(arr[0].split(" ")[0], arr[1].split(" ")[0])){
                    System.out.println(arr[0]);
                    break;
                }else{
                    System.out.println(arr[1]);
                }
            }else if(arr[0].split(" ").length == 2 && arr[1].split(" ").length == 2){
                if(compare(arr[0].split(" ")[0], arr[1].split(" ")[0])){
                    System.out.println(arr[0]);
                }else{
                    System.out.println(arr[1]);
                }
            }else if(arr[0].split(" ").length == 1 && arr[1].split(" ").length == 1){
                if(compare(arr[0].split(" ")[0], arr[1].split(" ")[0])){
                    System.out.println(arr[0]);
                }else{
                    System.out.println(arr[1]);
                }
            }else{
                System.out.println("ERROR");
            }
        }
    }
    public static boolean compare(String ch1, String ch2) {
        if(ch1.equals("JOKER")){
            return true;
        }
        if(ch2.equals("JOKER")){
            return false;
        }
        if(ch1.equals("joker")){
            return true;
        }
        if(ch2.equals("joker")){
            return false;
        }
        if (ch1.equals("2")) {
            return true;
        }
        if (ch2.equals("2")) {
            return false;
        }
        if (ch1.equals("A")) {
            return true;
        }
        if (ch2.equals("A")) {
            return false;
        }
        if (ch1.length() == 1&&ch1.charAt(0) >= 'J' && ch1.charAt(0) <= 'K') {
            if (ch2.length() == 1&& ch2.charAt(0) >= 'J' && ch2.charAt(0) <= 'K') {
                if (ch1.charAt(0) > ch2.charAt(0)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        }
        if (ch2.length() == 1&&ch2.charAt(0) >= 'J' && ch2.charAt(0) <= 'K') {
            if (ch1.length() == 1&& ch1.charAt(0) >= 'J' && ch1.charAt(0) <= 'K') {
                if (ch1.charAt(0) > ch2.charAt(0)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return Integer.parseInt(ch1) > Integer.parseInt(ch2);
    }
}

全部评论

相关推荐

01-05 09:14
同济大学 Java
心碎一号线:我要是9✌🏻我就选保研,保研了大四再找实习,实习之后,如果觉得自己不适合互联网工作模式,还能有其他选择,如果实习后决定了走互联网,也能提升学历提高竞争力
点赞 评论 收藏
分享
01-26 19:51
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务