题解 | #字符串加解密# 格式比较简洁优雅

字符串加解密

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();

            for (int i = 0; i < str1.length(); i++) {
                sb1.append(encrypt(str1.charAt(i)));
            }

            for (int i = 0; i < str2.length(); i++) {
                sb2.append(decrypt(str2.charAt(i)));
            }

            System.out.println(sb1);
            System.out.println(sb2);
        }
    }

    //加密
    public static char encrypt(char c) {
        if (c >= 'A' && c <= 'Z') {
            c = (char) ((c - 'A' + 1) % 26 + 'a');
        } else if (c >= 'a' && c <= 'z') {
            c = (char) ((c - 'a' + 1) % 26 + 'A');
        } else if (c <= '9') {
            c = (char) ((c - '0' + 1) % 10 + '0');
        }
        return c;
    }

    //解密
    public static char decrypt(char c) {
        if (c >= 'A' && c <= 'Z') {
            c = (char) ((c - 'A' - 1 + 26) % 26 + 'a');
        } else if (c >= 'a' && c <= 'z') {
            c = (char) ((c - 'a' - 1 + 26) % 26 + 'A');
        } else if (c <= '9') {
            c = (char) ((c - '0' - 1 + 10) % 10 + '0');
        }
        return c;
    }
}

全部评论

相关推荐

10-22 15:25
门头沟学院 C++
种花网友小松:求求你别发了,我几乎都快嫉妒得疯了,倒在床上蒙住被子就开始抱着枕头尖叫流泪,嘴里一边喊着卧槽卧槽,一边又忍着,我边发边哭,打字的手都是抖的,后来我的手抖得越来越厉害,从心头涌起的思想、情怀和梦想,这份歆羡和悔恨交织在一起,我的笑还挂在脸上,可是眼泪一下子就掉下来了。求你了别发了,我生活再难再穷我都不会觉得难过,只有你们发这种东西的时候,我的心里像被刀割一样的痛,打着字泪水就忍不住的往下流。
我的求职进度条
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务