题解 | #简单密码#

简单密码

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("abc", 2);
        map.put("def", 3);
        map.put("ghi", 4);
        map.put("jkl", 5);
        map.put("mno", 6);
        map.put("pqrs", 7);
        map.put("tuv", 8);
        map.put("wxyz", 9);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            if (c >= 'a' && c <= 'z') {
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if (entry.getKey().contains(String.valueOf(c))) {
                        sb.append(entry.getValue());
                        break;
                    }
                }
            } else if (c >= 'A' && c <= 'Y') {
                int convert = (int) c + 33;
                sb.append((char) convert);
            } else if (c == 'Z') {
                sb.append('a');
            } else {
                sb.append(c);
            }
        }
        System.out.println(sb);
    }
}
全部评论

相关推荐

奔跑的suechil...:怎么评论区这么多打广告的 1.项目考虑是两个,可以加个项目 2.bg一般的话,不建议外卖加点评,99%都过不了简历 3.找项目要么是自己找github好点的开源,要么是评论区找广告去跟着,要么就是星球找项目了 加油友友
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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