题解 | #字符串加解密#

字符串加解密

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        while (scanner.hasNext()) {
            String S = scanner.next();
            String T = scanner.next();
            jiami(S);
            jiemi(T);
        }
    }

    public static void jiami(String S) {
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            if (Character.isLetter(c)) {
                 System.out.print(change(c));
             } else if (Character.isDigit(c)){
                 System.out.print(change(c));
             } else {
                System.out.print(c);
            }
        }
        System.out.println();
    }

    public static void jiemi(String S) {
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            if (Character.isLetter(c)) {
                System.out.print(change2(c));
            } else if (Character.isDigit(c)){
                System.out.print(change2(c));
            } else {
                System.out.print(c);
            }

        }
        System.out.println();
    }

    public static char change(char c) {
        if (Character.isLetter(c)) {
            if (c=='Z') return 'a';
            else if (c=='z') return 'A';
            else if (c>='a'&&c<'z') return Character.toUpperCase((char) (c+1));
            else {
                return Character.toLowerCase((char) (c+1));
            }
        } else if (Character.isDigit(c)){
            if (c=='9') return '0';
            else return (char) (c+1);
        }
        return c;
    }

    public static char change2(char c) {
        if (Character.isLetter(c)) {
            if (c=='A') return 'z';
            else if (c=='a') return 'Z';
            else if (c>'a'&&c<='z') return Character.toUpperCase((char) (c-1));
            else {
                return Character.toLowerCase((char) (c-1));
            }
        } else if (Character.isDigit(c)){
            if (c=='0') return '9';
            else return (char) (c-1);
        }
        return c;
    }
}

全部评论

相关推荐

预计下个星期就能开奖吧,哪位老哥来给个准信
华孝子爱信等:对接人上周说的是这周
点赞 评论 收藏
分享
10-15 10:57
已编辑
武昌理工学院 FPGA工程师
狠赚笔第一人:老哥学院本没实习还想拿13k学Java狠赚笔呢
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:48
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务