题解 | #简单密码#

简单密码

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        char[] strChar = str.toCharArray();
        int len = strChar.length;
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<len;i++){
            char index = strChar[i];
            if(index>=65 && index <90){
                int change = index+33;
                builder.append((char)change);
            } else if(index == 'Z'){
                builder.append('a');
            } else if(index>=97 && index<=122){
                if(index=='a'|| index=='b'||index=='c'){
                    builder.append('2');
                }
                if(index=='d'|| index=='e'||index=='f'){
                    builder.append('3');
                }
                if(index=='g'|| index=='h'||index=='i'){
                    builder.append('4');
                }
                if(index=='j'|| index=='k'||index=='l'){
                    builder.append('5');
                }
                if(index=='m'|| index=='n'||index=='o'){
                    builder.append('6');
                }
                if(index=='p'|| index=='q'||index=='r'||index=='s'){
                    builder.append('7');
                }
                if(index=='t'|| index=='u'||index=='v'){
                    builder.append('8');
                }
                if(index=='w'|| index=='x'||index=='y'||index=='z'){
                    builder.append('9');
                }
            } else {
                builder.append(strChar[i]);
            }
        }
        System.out.print(builder);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务