• Java 课堂作业 加密与解密


    1.设计思路

    首先根据提示输入一段字符串

    利用charAt()将字符串的每个字符分解出来,要加密的话转换成int类型后加3,解密的话转换成int类型后减3,然后再转化为char类型

    新定义一个字符串变量,将转换后的char链接起来输出

    2.流程图

    3.源代码

    package test;
    import java.util.Scanner;
    class Cipher
    {
    Scanner input=new Scanner(System.in);
    void incipher()
    {
    System.out.println("请输入你想要加密的字符串:");
    String str=input.nextLine();
    String str2="";
    for(int i=0;i<str.length();i++)
    {
    int n=(int)str.charAt(i)+3;
    str2=str2+(char)n;
    }
    System.out.println("加密后的字符串为:"+str2);
    }
    void outcipher()
    {
    System.out.println("请输入你想要解密的字符串:");
    String str=input.nextLine();
    String str2="";
    for(int i=0;i<str.length();i++)
    {
    int n=(int)str.charAt(i)-3;
    str2=str2+(char)n;
    }
    System.out.println("解密后的字符串为:"+str2);
    }
    }
    public class Classtest5
    {
    public static void main(String[] args)
    {
    Cipher c=new Cipher();
    Scanner input=new Scanner(System.in);
    int choice;
    do
    {
    System.out.println("请选择你想要加密还是解密:1.加密 2.解密");
    int choise=input.nextInt();
    if(choise==1)
    {
    c.incipher();
    }
    else if(choise==2)
    {
    c.outcipher();
    }
    else
    System.out.println("输入错误!");
    System.out.println("是否继续?1.是 2.否");
    choice=input.nextInt();
    }while(choice==1);

    }

    }

    4.截图

  • 相关阅读:
    C#+Arcengine创建内存图层
    Creating a Feature Set via C#
    ArcGIS Server for JavaScript api安装部署
    Lucene.Net 3.0.3如何从TokenStream中获取token对象
    MMSEG 中文算法说明
    java DotNet char 代码对应
    Lucene.Net 3.0.3如何从TokenStream中获取token对象
    java DotNet char 代码对应
    9.7
    9.6
  • 原文地址:https://www.cnblogs.com/sakura--/p/7731904.html
Copyright © 2020-2023  润新知