题解 | #字符串加解密#

字符串加解密

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);
        while (scanner.hasNext()){
            String incode = scanner.nextLine();
            String outcode = scanner.nextLine();
            char[] charin = incode.toCharArray();
            char[] charout = outcode.toCharArray();
            String incode1 = Main.incode(charin);
            String outcode1 = Main.outcode(charout);
            System.out.println(incode1);
            System.out.println(outcode1);

        }
    }
    public static String incode(char[] a){
        for (int i = 0; i < a.length; i++) {
            if (Character.isLetter(a[i])){
                if (Character.isUpperCase(a[i])){
                    a[i] = Character.toLowerCase(a[i]);
                    if (String.valueOf(a[i]).matches("[z]")){
                        a[i]='a';
                    }else {
                        char c = (char) (Integer.valueOf(a[i]) + 1);
                        a[i]=c;
                    }
                }else {
                    a[i] = Character.toUpperCase(a[i]);
                    if (String.valueOf(a[i]).matches("[Z]")){
                        a[i]='A';
                    }else {
                        char c = (char) (Integer.valueOf(a[i]) + 1);
                        a[i]=c;
                    }
                }
            } else if (Character.isDigit(a[i])) {
                if (a[i]=='9'){
                    a[i]='0';
                }else{
                    a[i]=(char)(Integer.valueOf(a[i])+1);
                }
            }
        }
        String x="";
        for (int i = 0; i <a.length; i++) {
            x=x+a[i];
        }
        return x;
    }
    public static String outcode(char[] a){
        for (int i = 0; i < a.length; i++) {
            if (Character.isLetter(a[i])){
                if (Character.isUpperCase(a[i])){
                    a[i] = Character.toLowerCase(a[i]);
                    if (String.valueOf(a[i]).matches("[a]")){
                        a[i]='z';
                    }else {
                        char c = (char) (Integer.valueOf(a[i]) - 1);
                        a[i]=c;
                    }
                }else {
                    a[i] = Character.toUpperCase(a[i]);
                    if (String.valueOf(a[i]).matches("[A]")){
                        a[i]='Z';
                    }else {
                        char c = (char) (Integer.valueOf(a[i]) - 1);
                        a[i]=c;
                    }
                }
            } else if (Character.isDigit(a[i])) {
                if (a[i]=='0'){
                    a[i]='9';
                }else{
                    a[i]=(char)(Integer.valueOf(a[i])-1);
                }
            }
        }
        String x="";
        for (int i = 0; i <a.length; i++) {
            x=x+a[i];
        }
        return x;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
hso_:哈哈哈哈哈哈我没offer一样在同一道题开喷了
投递深圳同为数码等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务