题解 | #简单密码#

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static TreeMap<Character, Integer> map = new TreeMap<>();

    static {
        map.put('a', 2);
        map.put('b', 2);
        map.put('c', 2);
        map.put('d', 3);
        map.put('e', 3);
        map.put('f', 3);
        map.put('g', 4);
        map.put('h', 4);
        map.put('i', 4);
        map.put('j', 5);
        map.put('k', 5);
        map.put('l', 5);
        map.put('m', 6);
        map.put('n', 6);
        map.put('o', 6);
        map.put('p', 7);
        map.put('q', 7);
        map.put('r', 7);
        map.put('s', 7);
        map.put('t', 8);
        map.put('u', 8);
        map.put('v', 8);
        map.put('w', 9);
        map.put('x', 9);
        map.put('y', 9);
        map.put('z', 9);
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String line = in.nextLine();
        StringBuilder sb = new StringBuilder(line.length());
        for(char c : line.toCharArray()) {
            if (c > 96) {   // 小写字母
                sb.append(map.get(c));
            } else if(c>64) {   // 大写字母
                sb.append(upperToLower(c));
            } else {    // 数字
                sb.append(c);
            }
        }

        System.out.println(sb.toString());
        in.close();
    }

    private static char upperToLower(char upperChar) {

        if('Z' == upperChar) {
            return 'a';
        }

        return (char) (upperChar + 33);
    }
}

#每日一题#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务