zoukankan      html  css  js  c++  java
  • 凯撒加密作业

    设计思想:

    创立一个类class Encipherment

    方法二个:

              public void encrypt(String sr);//加密方法

              Public void decode(String sr);//解密方法

    在主函数中输入一段字符串,然后调用类Encipherment中的方法解决问题。

    程序流程图:

     

    源代码:

    package 凯撒加密;
    
    import java.util.Scanner;
    
    public class Homework
    
    {
    
        public static void main(String args[])
    
        {
    
         Scanner input=new Scanner(System.in);
    
         Encipherment en=new Encipherment();
    
         String str;
    
        
    
         System.out.println("输入一段字符串:");
    
         str=input.nextLine();
    
         en.encrypt(str);
    
        
    
         System.out.println();
    
        
    
         System.out.println("再次输入一段字符串:");
    
         str=input.nextLine();
    
         en.decode(str);
    
        
    
        }
    
    }
    
    class Encipherment
    
    {
    
    public void encrypt(String sr)//加密方法
    
    {
    
    String newStr="";
    
    char getCharAt=0;
    
    for(int i=0;i<sr.length();i++)
    
    {
    
    if(sr.charAt(i)>=65&&sr.charAt(i)<=87||sr.charAt(i)>=97&&sr.charAt(i)<=119)
    
    {
    
    getCharAt=(char)(sr.charAt(i)+3);
    
    }
    
    else if(sr.charAt(i)>=88&&sr.charAt(i)<90||sr.charAt(i)>=120&&sr.charAt(i)<=122)
    
    {
    
    getCharAt=(char)(sr.charAt(i)-23);
    
    }
    
    newStr=newStr+getCharAt;
    
    }
    
    System.out.println("加密后的字符串为"+newStr);
    
    }
    
    public void decode(String sr)//解密方法
    
    {
    
     
    
    String newStr="";
    
    char getCharAt=0;
    
    for(int i=0;i<sr.length();i++)
    
    {
    
    if(sr.charAt(i)>=65&&sr.charAt(i)<=87||sr.charAt(i)>=97&&sr.charAt(i)<=119)
    
    {
    
    getCharAt=(char)(sr.charAt(i)-3);
    
    }
    
    else if(sr.charAt(i)>=88&&sr.charAt(i)<90||sr.charAt(i)>=120&&sr.charAt(i)<=122)
    
    {
    
    getCharAt=(char)(sr.charAt(i)+23);
    
    }
    
    newStr=newStr+getCharAt;
    
    }
    
    System.out.println("解密后的字符串为"+newStr);
    
    }
    
    }

    结果截图:

     

  • 相关阅读:
    PTA|基础编程题目集|7-8
    PTA|基础编程题目集|7-7
    PTA|基础编程题目集|7-11
    PTA|基础编程题目集|7-3
    PTA|基础编程题目集|7-2
    PTA|基础编程题目集|7-1
    PTA|基础编程题目集|7-5
    PTA|基础编程题目集|7-6
    scrapy-redis使用以及剖析
    Python数据库连接池DBUtils
  • 原文地址:https://www.cnblogs.com/ssyh/p/7729734.html
Copyright © 2011-2022 走看看