Java写题解的第3天 | #字符串加解密#

字符串加解密

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

模拟过程即可

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String encode = "";
        while ((encode = br.readLine()) != null) {
            String decode = br.readLine();
            System.out.println(doEncode(encode));
            System.out.println(doDecode(decode));
        }
        br.close();
    }

    public static String doEncode(String s) {
        StringBuilder sb = new StringBuilder();
        for(char c: s.toCharArray()) {
            if ('0' <= c && c <= '8') {
                sb.append((char)(c+1));
            } else if (c == '9') {
                sb.append('0');
            } else if ('a' <= c && c <= 'y') {
                sb.append((char)(c + 1 - 'a' + 'A'));
            } else if (c == 'z') {
                sb.append('A');
            } else if ('A' <= c && c <= 'Y') {
                sb.append((char)(c + 1 - 'A' + 'a'));
            } else if (c == 'Z') {
                sb.append('a');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String doDecode(String s) {
        StringBuilder sb = new StringBuilder();
        for(char c: s.toCharArray()) {
            if ('1' <= c && c <= '9') {
                sb.append((char)(c-1));
            } else if (c == '0') {
                sb.append('9');
            } else if ('b' <= c && c <= 'z') {
                sb.append((char)(c - 1 - 'a' + 'A'));
            } else if (c == 'a') {
                sb.append('Z');
            } else if ('B' <= c && c <= 'Z') {
                sb.append((char)(c - 1 - 'A' + 'a'));
            } else if (c == 'A') {
                sb.append('z');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
全部评论

相关推荐

10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务