题解 | #简单密码#

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac?tpId=37&tqId=21244&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static char[] arr = new char[]{
        '2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6',
        '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'
    };
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        char[] chars = in.nextLine().toCharArray();

        for(char ch : chars){
            if(Character.isLowerCase(ch)){
                System.out.print(arr[ch - 'a']);
                continue;
            }
            if(Character.isUpperCase(ch) && ch != 'Z'){
                ch = (char) (Character.toLowerCase(ch) + 1);
                System.out.print(ch);
                continue;
            }
            if(ch == 'Z'){
                System.out.print('a');
                continue;
            }
            System.out.print(ch);
        }
    }
}

全部评论

相关推荐

面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务