题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

原来大家都是这种笨方法啊

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String b = in.nextLine();
            System.out.println(jia(a));
            System.out.println(jie(b));
        }
    }
    public static String jia(String s) {
        char[] ch = s.toCharArray();
        //StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (Character.isLowerCase(c)) {
                if (c == 'z') {
                    ch[i] = 'A';
                } else {
                    ch[i] = (char)((int)c - 31);
                }
            } else if (Character.isUpperCase(c)) {
                if (c == 'Z') {
                    ch[i] = 'a';
                } else {
                    ch[i] = (char)((int)c + 33);
                }
            } else if (Character.isDigit(c)) {
                if (c == '9') {
                    ch[i] = '0';
                } else {
                    ch[i] = (char)((int)c + 1);
                }
            }
        }
        return String.valueOf(ch);
    }
    public static String jie(String s) {
        char[] ch = s.toCharArray();
        //StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (Character.isLowerCase(c)) {
                if (c == 'a') {
                    ch[i] = 'Z';
                } else {
                    ch[i] = (char)((int)c - 33);
                }
            } else if (Character.isUpperCase(c)) {
                if (c == 'A') {
                    ch[i] = 'z';
                } else {
                    ch[i] = (char)((int)c + 31);
                }
            } else if (Character.isDigit(c)) {
                if (c == '0') {
                    ch[i] = '9';
                } else {
                    ch[i] = (char)((int)c -1 );
                }
            }
        }
        return String.valueOf(ch);
    }

}

全部评论

相关推荐

11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务