题解 | 简单密码

简单密码

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        
        // for(int i = 0;i < str.length();i++){
        //     char c = str.charAt(i);
        //     if(c == 'a' || c == 'b' || c == 'c'){
        //         c = '2';
        //     }
        //     else if (c == 'd' || c == 'e' || c == 'f'){
        //         c = '3';
        //     }
        //     else if (c == 'g' || c == 'h' || c == 'i'){
        //         c = '4';
        //     }
        //     else if (c == 'j' || c == 'k' || c == 'l'){
        //         c = '5';
        //     }
        //     else if (c == 'm' || c == 'n' || c == 'o'){
        //         c = '6';
        //     }
        //     else if (c == 'p' || c == 'q' || c == 'r' || c == 's'){
        //         c = '7';
        //     }
        //     else if (c == 't' || c == 'u' || c == 'v'){
        //         c = '8';
        //     }
        //     else if (c == 'x' || c == 'y' || c == 'z' || c == 'w'){
        //         c = '9';
        //     }
        //     else if(c >= 'A' && c < 'Z'){
        //         c = (char)(c + 32 + 1);
        //     }
        //     else if(c == 'Z'){
        //         c = 'a';
        //     }
        //     System.out.print(c);
        String res = "";
        Map<Character,Character> map = new HashMap<>();
        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');
        for(int i = 0;i < str.length(); i++){
            if(map.get(str.charAt(i)) != null){
                res = res + map.get(str.charAt(i));
            }
            else if(str.charAt(i) >= 'A' && str.charAt(i) < 'Z'){
                res = res + (char) (str.charAt(i) + 32 + 1);
            }
            else if(str.charAt(i) == 'Z'){
                res =res + 'a';
            }
            else{
                res = res + str.charAt(i);
            }
        }
        System.out.println(res);
        }
    }

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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