题解 | #字符串加解密#

字符串加解密

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

笨办法,捞的不行

import java.util.*;

/**
 * @Date:2021/12/29 13:06
 * @Author:NANDI_GUO
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String str1 = sc.nextLine();
            System.out.println(passw(str));
            System.out.println(hack(str1));
        }
        

    }
    static String passw(String str) {
        char[] ch = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < ch.length; i++) {
            if (ch[i]>='a' && ch[i]<'z') {
                ch[i] = (char) (ch[i]-32+1);
            }else if (ch[i]>='A' && ch[i]< 'Z'){
                ch[i] = (char) (ch[i] + 32 + 1);
            }else if(ch[i] == 'z'){
                ch[i] = 'A';
            }else if (ch[i] == 'Z'){
                ch[i] = 'a';
            }else if(!Character.isLetter(ch[i])){
                if (ch[i] == '9'){
                    ch[i] = '0';
                }else{
                    ch[i] = (char)(ch[i]+1);
                }
            }
            sb.append(ch[i]);
        }
        return sb.toString();
    }
    static String hack(String str){
        char[] ch = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < ch.length; i++) {
            if (ch[i]>'A' && ch[i]<='Z') {
                ch[i] = (char) (ch[i] -1 + 32);
            }else if (ch[i]>'a' && ch[i]<= 'z'){
                ch[i] = (char) (ch[i] -1 - 32);
            }else if(ch[i] == 'A'){
                ch[i] = 'z';
            }else if (ch[i] == 'a'){
                ch[i] = 'Z';
            }else if(!Character.isLetter(ch[i])){
                if (ch[i] == '0'){
                    ch[i] = '9';
                }else{
                    ch[i] = (char)(ch[i]-1);
                }
            }
            sb.append(ch[i]);
        }
        return sb.toString();
    }
}
全部评论

相关推荐

10-30 22:18
已编辑
毛坦厂中学 C++
点赞 评论 收藏
分享
10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务