题解 | #简单密码#

简单密码

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

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
	  // 按键对应关系塞进去
        HashMap<Integer,String> map = new HashMap<>();
        map.put(2,"abc");
        map.put(3,"def");
        map.put(4,"ghi");
        map.put(5,"jkl");
        map.put(6,"mno");
        map.put(7,"pqrs");
        map.put(8,"tuv");
        map.put(9,"wxyz");
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            for(char i : a.toCharArray()){
			  // 判断数字
                if(i<='9' && i>='0'){
                    System.out.print(i);
                    continue;
			  // 判断大写
                }else if(i>='A' && i<='Z'){
                    if(i=='Z'){
                        System.out.print('a');
                        continue;
                    }else{
                        System.out.print(((char)(i+1+32))); // ascii 大写+32变小写
                        continue;
                    }
			  // 判断小写
                }else{
                    for(int key : map.keySet()){
                        if(map.get(key).contains(i+"")){
                            System.out.print(key);
                        }
                    }
                }      
            }
        }
    }
}

全部评论

相关推荐

海螺很能干:每次看到这种简历都没工作我就觉得离谱
点赞 评论 收藏
分享
自由水:这HR已经很好了,多的是已读不回和不读了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务