Java(简单密码)

简单密码

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

题目描述 “为了方便记忆,他通过一种算法把这个密码变换成” 错了吧,明明是用算法把明文变换成密码🙄

解题思路:通过多个分支判断语句将字符对应转换,大小写转换不用查ASCII码表,通过字符的加减就能实现。

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String cleartext = in.nextLine();    //读取明文
            
            StringBuilder builder = new StringBuilder();
            for(int i=0; i < cleartext.length(); i++){
                //小写字母转换为数字
                if(cleartext.charAt(i)>='a' && cleartext.charAt(i)<='c')
                    builder.append('2');
                else if(cleartext.charAt(i)>='d' && cleartext.charAt(i)<='f')
                    builder.append('3');
                else if(cleartext.charAt(i)>='g' && cleartext.charAt(i)<='i')
                    builder.append('4');
                else if(cleartext.charAt(i)>='j' && cleartext.charAt(i)<='l')
                    builder.append('5');
                else if(cleartext.charAt(i)>='m' && cleartext.charAt(i)<='o')
                    builder.append('6');
                else if(cleartext.charAt(i)>='p' && cleartext.charAt(i)<='s')
                    builder.append('7');
                else if(cleartext.charAt(i)>='t' && cleartext.charAt(i)<='v')
                    builder.append('8');
                else if(cleartext.charAt(i)>='w' && cleartext.charAt(i)<='z')
                    builder.append('9');
                
                //大写字母'A'~'Y'转换
                else if(cleartext.charAt(i)>='A' && cleartext.charAt(i)<='Y')
                    builder.append((char)(cleartext.charAt(i)+'a'-'A'+1));
                //大写字母'Z'单独处理
                else if(cleartext.charAt(i) == 'Z')
                    builder.append('a');
                
                //其他字符不做变换
                else
                    builder.append(cleartext.charAt(i));
            }
            System.out.println(builder.toString());
        }
    }
}


全部评论
比说的对,我说我怎么读不懂题
点赞
送花
回复 分享
发布于 2022-02-22 13:42
一模一样~ 打表哥~
点赞
送花
回复 分享
发布于 2022-04-16 16:56
国泰君安
校招火热招聘中
官网直投
厉害!
点赞
送花
回复 分享
发布于 2022-09-01 23:31 北京
直接用toLowerCase() 为啥无法变小写?
点赞
送花
回复 分享
发布于 2023-03-07 17:49 陕西
对哦,这么写就不用查表了,受教了。
点赞
送花
回复 分享
发布于 2023-04-29 10:23 广西

相关推荐

24 3 评论
分享
牛客网
牛客企业服务