蓝桥杯测试题--9-2文本加密

试题 算法提高 9-2 文本加密


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

public class Main
{
    static char [] art = new char[100];
    
    public static void print()
    {
        for(int i=0;i<art.length;i++)
        {
            System.out.print(art[i]);
        }
        System.out.println();
    }
    
    public static void fun(int i)
    {
        if(art[i] =='Z')
        {
            art[i] = 'a';
        }
        else if(art[i] == 'z')
        {
            art[i] = 'A';
        }
        else {
            art[i] = (char) (art[i]+1);
        }
    
    }
    
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext())
        {
            art = scanner.next().toCharArray();
            for(int i=0;i<art.length;i++)
            {
                if((art[i]>='a'&&art[i]<='z')|| (art[i]>='A'&&art[i]<='Z'))
                {
                    fun(i);
                }
            }
            print();
            
        }
        

    }

}

 

posted @ 2020-04-02 12:27  池塘之底  阅读(184)  评论(0编辑  收藏  举报