题解 | #字符串加解密#

字符串加解密

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

import java.util.Scanner;

public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); String s1= sc.nextLine(); String s2= sc.nextLine();

    char[] chars = s1.toCharArray();

    for (int i = 0; i < chars.length; i++) {
        if(Character.isDigit(chars[i])) {
            if(chars[i] == '9'){
                chars[i]='0';
            }else{
                chars[i] = (char) (chars[i] + 1);

            }

        }else if(Character.isLetter(chars[i])){
            if(Character.isLowerCase(chars[i])){
                char c = Character.toUpperCase(chars[i]);
                if(c == 'Z'){
                    chars[i]='A';
                }else{
                    chars[i]= (char) (c+1);
                }

            }else{

                char c = Character.toLowerCase(chars[i]);
                if(c == 'z'){
                    chars[i]='a';
                }else{
                    chars[i]= (char) (c+1);
                }

            }



        }
    }
    char[] chars1 = s2.toCharArray();
    for (int i = 0; i < chars1.length; i++) {
        if(Character.isDigit(chars1[i])) {
            if(chars1[i] == '0'){
                chars1[i]='9';
            }else{
                chars1[i] = (char) (chars1[i] -1);

            }

        }else if(Character.isLetter(chars1[i])){
            if(Character.isLowerCase(chars1[i])){
                char c = Character.toUpperCase(chars1[i]);
                if(c == 'A'){
                    chars1[i]='Z';
                }else{
                    chars1[i]= (char) (c-1);
                }

            }else{
                char c = Character.toLowerCase(chars1[i]);
                if(c =='a'){
                    chars1[i]='z';
                }else{
                    chars1[i]= (char) (c-1);
                }

            }



        }
    }
    StringBuilder sb1 = new StringBuilder();
    for(char c:chars){
        sb1.append(c);
    }
    StringBuilder sb2 = new StringBuilder();
    for(char c:chars1){
        sb2.append(c);
    }
    System.out.println(sb1.toString());
    System.out.println(sb2.toString());

}

}

全部评论

相关推荐

11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务