zoukankan      html  css  js  c++  java
  • 04-String——课后作业1:字串加密

    题目:请编写一个程序,加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图。

    程序设计思想:首先由用户选择是加密还是解密,利用String类中的charAt函数依次取出字串中的字符,如果加密(解密)就把取出的字符利用强制转换转换成int型之后如果是XYZ(ABC)就减(加)23其他的加(减)3然后再转换成char类型,然后定义一个char类型数组用来储存转换之后的字符。

    程序流程图:这里只写了破解时的流程图,加密时基本一样。

    程序源代码:

      1 import java.util.Scanner;
      2 
      3 public class SwitchPassword {
      4     
      5     Scanner input = new Scanner(System.in);
      6     
      7     //加密字串
      8     public void encrypt()
      9     
     10     {
     11         
     12         System.out.print("请输入要加密的字串:");
     13         
     14         String str = input.next();
     15         
     16         char encryptstr[] = new char[str.length()];
     17         
     18         for(int i = 0;i < str.length();i ++)
     19             
     20         {
     21             
     22             if(str.charAt(i) == 'X' || str.charAt(i) == 'Y' || str.charAt(i) == 'Z')
     23                 
     24             {
     25                 
     26                 encryptstr[i] = (char)((int)str.charAt(i) - 23);
     27                 
     28             }
     29             
     30             else
     31                 
     32                 encryptstr[i] = (char)((int)str.charAt(i) + 3);
     33             
     34         }
     35         
     36         System.out.print("加密之后的字串为:");
     37         
     38         for(int i = 0;i < str.length();i ++)
     39             
     40         {
     41             
     42             System.out.print(encryptstr[i]);
     43             
     44         }
     45         
     46     }
     47     
     48     //破解字串
     49     public void crack()
     50         
     51     {
     52         
     53         System.out.print("请输入要破解的字串:");
     54             
     55         String str = input.next();
     56             
     57         char crackstr[] = new char[str.length()];
     58             
     59         for(int i = 0;i < str.length();i ++)
     60         
     61         {
     62                 
     63             if(str.charAt(i) == 'A' || str.charAt(i) == 'B' || str.charAt(i) == 'C')
     64                     
     65             {
     66                     
     67                 crackstr[i] = (char)((int)str.charAt(i) + 23);
     68                     
     69             }
     70                 
     71             else
     72                     
     73                 crackstr[i] = (char)((int)str.charAt(i) - 3);
     74                 
     75         }
     76             
     77         System.out.print("破解之后的字串为:");
     78             
     79         for(int i = 0;i < str.length();i ++)
     80                 
     81         {
     82                 
     83             System.out.print(crackstr[i]);
     84                 
     85         }
     86             
     87 }
     88     
     89     public static void main(String[] args) {
     90         
     91         Scanner input = new Scanner(System.in);
     92         
     93         SwitchPassword s = new SwitchPassword();
     94         
     95         System.out.println("请选择: 1.加密 	2.破解");
     96         
     97         int ch = input.nextInt();
     98         
     99         switch(ch)
    100         
    101         {
    102         
    103         case 1:
    104             
    105             s.encrypt();break;
    106             
    107         case 2:
    108             
    109             s.crack();break;
    110         
    111         }
    112         
    113     }
    114 
    115 }

    程序运行结果:

  • 相关阅读:
    构建工具
    Ajax跨域问题
    Flex 布局教程:语法篇
    Linux常用命令
    JavaScript正则表达式
    jQuery基础(四)动画
    前端面试问题汇总(一)
    jQuery基础(三)事件
    JavaScript中的基本数据类型
    Django积木块二——邮箱
  • 原文地址:https://www.cnblogs.com/guo-xu/p/7731899.html
Copyright © 2011-2022 走看看