算法笔记_086:蓝桥杯练习 9-2 文本加密(Java)

目录

1 问题描述

2 解决方案

 


1 问题描述

问题描述
  先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:"A"转化"B","B"转化为"C",... ..."Z"转化为"a","a"转化为"b",... ..., "z"转化为"A",其它字符不加密。编写程序,加密给定字符串。
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
  输入数据中每一个数的范围。
  例:50个字符以内无空格字符串。

 


2 解决方案

 

 

具体代码如下:

import java.util.Scanner;

public class Main {
    
    public void EncryptChar(String A) {
        if(A.length() < 1)
            return;
        StringBuilder result = new StringBuilder("");
        char[] arrayA = A.toCharArray();
        for(int i = 0;i < A.length();i++) {
            char temp = arrayA[i];
            if(temp >= 'a' && temp < 'z' || temp >= 'A' && temp < 'Z') {
                temp = (char) (temp + 1);    
            } else if(temp == 'Z') {
                temp = 'a';
            } else if(temp == 'z') {
                temp = 'A';
            }
            result.append(temp);
        }
        System.out.println(result);
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        String A = in.nextLine();
        test.EncryptChar(A);
        
    }
}

 

posted @ 2017-03-17 09:11  舞动的心  阅读(442)  评论(0编辑  收藏  举报