题解 | #简单密码# string.replace()不是

简单密码

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

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String,String> map = new HashMap<>();
        map.put("A","b");
        map.put("B","c");
        map.put("C","d");
        map.put("D","e");
        map.put("E","f");
        map.put("F","g");
        map.put("G","h");
        map.put("H","i");
        map.put("I","j");
        map.put("J","k");
        map.put("K","l");
        map.put("L","m");
        map.put("M","n");
        map.put("N","o");
        map.put("O","p");
        map.put("P","q");
        map.put("Q","r");
        map.put("R","s");
        map.put("S","t");
        map.put("T","u");
        map.put("U","v");
        map.put("V","w");
        map.put("W","x");
        map.put("X","y");
        map.put("Y","z");
        map.put("Z","a");

        StringBuilder sb = new StringBuilder();

        String str = in.nextLine();
        for (int i = 0; i < str.length(); i++) {
            String s1 = str.charAt(i) + "";
            if (map.containsKey(s1)){
//                String s = map.get(s1);
//                str = str.replace(s1, s);//replace不是直接修改原字符串 而是重新生成字符串
                sb.append(map.get(s1));
                continue;
            }
            switch (str.charAt(i)){
                case 'a':
                case 'b':
                case 'c':
                    sb.append(2);
                    continue;
                case 'd':
                case 'e':
                case 'f':
                    sb.append(3);
                    continue;
                case 'g':
                case 'h':
                case 'i':
                    sb.append(4);
                    continue;
                case 'j':
                case 'k':
                case 'l':
                    sb.append(5);
                    continue;
                case 'm':
                case 'n':
                case 'o':
                    sb.append(6);
                    continue;

                case 'p':
                case 'q':
                case 'r':
                case 's':
                    sb.append(7);
                    continue;

                case 't':
                case 'u':
                case 'v':
                    sb.append(8);
                    continue;
                case 'w':
                case 'x':
                case 'y':
                case 'z':
                    sb.append(9);
                    continue;
                default:
                    sb.append(str.charAt(i));
                    break;
            }


        }
        System.out.println(sb);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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