题解 | #字符串加解密#

字符串加解密

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String pas1 = sc.nextLine();
        String pas2 = sc.nextLine();
        System.out.println(encode(pas1));
        System.out.println(decode(pas2));

    }
    public static String encode(String pas) {
        String res = "";
        char[] chs = pas.toCharArray();
        for (char c : chs) {
            if (Character.isLetter(c)) {
                if (c >= 'a' && c <= 'z') {
                    if (c == 'z') {
                        res += 'A';
                    } else {
                        res += Character.toUpperCase((char)(c + 1));
                    }
                } else {
                    if (c == 'Z') {
                        res += 'a';
                    } else {
                        res += Character.toLowerCase((char)(c + 1));
                    }
                }
            }
            if (Character.isDigit(c)) {
                if (c == '9') {
                    res += 0;
                } else {
                    res += (char)(c + 1);
                }
            }

        }
        return res;
    }
    public static String decode(String pas) {
        String res = "";
        char[] chs = pas.toCharArray();
        for (char c : chs) {
            if (Character.isLetter(c)) {
                if (c >= 'a' && c <= 'z') {
                    if (c == 'a') {
                        res += 'Z';
                    } else {
                        res += Character.toUpperCase((char)(c - 1));
                    }
                } else {
                    if (c == 'A') {
                        res += 'z';
                    } else {
                        res += Character.toLowerCase((char)(c - 1));
                    }
                }
            }
            if (Character.isDigit(c)) {
                if (c == '0') {
                    res += 9;
                } else {
                    res += (char)(c - 1);
                }
            }

        }
        return res;
    }
}

全部评论

相关推荐

虚闻松声:继续投吧。 简历没啥问题。很优秀。 拙见:自我评价没什么意义;试试转向Agent开发、大模型应用;别死磕传统Java开发。 免费修改简历,就业咨询,欢迎私信交流。
点赞 评论 收藏
分享
神哥不得了:首先我就是在成都,成都的互联网格外的卷,如果是凭现在的简历的话很难找到大厂,建议再添加一个高质量的项目上去,另外专业技能的话最好是超过每一条的一半
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务