每日作业20200618 - 密码加密

题目

密码加密

基础作业
    张三的加密方式是这样的:
    对于每个字母和数字, 按字母表/数字顺序往后挪5位
    符号不变
    如密码: AaZz09!!!
    加密后: FfEe54!!!
-----------------------------------------------------------
进阶作业
    张三的加密方式是这样的:
    对位于每个字母, 按字母表往后挪x
    x为该字母在字母表中的位置(如a 加密后为b, b加密后为d, y加密后为x, z加密后为z)
    对于每个数字, 取其平方的个位(4 加密后为6, 8 加密后为4)
    符号不变
    如密码: AaZz09!!!
    加密后: BbZz01!!!

分析

    大写字母 A-Z:65-90,
    小写字母 a-z:97-122,
    数字     0-948-57

细节:每个字符列表自成闭环

代码

public class Homework0618 {
    public static void main(String[] args) {

        /**
         * 打印
         */
        Scanner sc = null;
        int num = 0;
        while (true){
            System.out.println("\n************************************************************");
            System.out.println("1、标准作业\t\t2、进阶作业\t\t3、退出");
            System.out.print("请输入对应编号,查看对应答案: ");
            sc = new Scanner(System.in);
            num = sc.nextInt(); //接收用户输入

            if (num == 1){
                System.out.println("\n******************标准作业******************");
                basics();   //调用方法
            }else if (num == 2){
                System.out.println("\n******************进阶作业******************");
                advanced(); //调用方法
            }else if (num == 3){
                System.out.println("\n******************系统退出******************");
                break;
            }
            else{
                System.out.println("输入错误,请重新输入");
            }
        }

    }

//**********************************************************************************************************************************

    /**
     * 方法--进阶作业
     */
    public static void advanced() {
        String psw = input();              //调用输入方法,得到合法的数字
        String encryption = method2(psw);       //调用阶乘方法,得到计算结果
        System.out.println( "\n初始密码: " + psw + "\t\t加密后为: " + encryption);   //输出结果
        /*是否继续计算*/
        if( judge() == true ){
            advanced();
        }
    }

    /**
     * 进阶作业的加密方法
     * @param psw
     * @return
     */
    public static String method2(String psw) {
        char[] ch = psw.toCharArray();  //将字符串转成字符数组
        char temp = ' ';    //定义临时变量
        int num = 0;

        for(int i = 0; i < ch.length; i++){     //遍历
            if (65 <= ch[i] && ch[i] <= 90){                  //大写字母 A-Z:65-90,
                temp = (char)(ch[i] - 64 + ch[i]);  //赋值给临时变量
                ch[i] = temp > 90 ? (temp -= 26) : temp;

            }else if(97 <= ch[i] && ch[i] <= 122) {           //小写字母 a-z:97-122,
                temp = (char)(ch[i] - 96 + ch[i]);  //赋值给临时变量
                ch[i] = temp > 122 ? (temp -= 26) : temp;

            }else if (48 <= ch[i] && ch[i] <= 57){            //数字 0-9:48-57
                num = (int) Math.pow(ch[i] - 48, 2) % 10;
                ch[i] = (char) (num + 48);

            }else {
                ch[i] = ch[i];  //符号不变
            }
        }
        String encryption = String.valueOf(ch); //转为字符串
        return encryption;
    }

//**********************************************************************************************************************************

    /**
     * 方法--标准作业
     */
    public static void basics() {
        String psw = input();              //调用输入方法,得到合法的数字
        String encryption = method1(psw);    //调用阶乘方法,得到计算结果
        System.out.println( "\n初始密码: " + psw + "\t\t加密后为: " + encryption);   //输出结果
        /*是否继续计算*/
        if( judge() == true ){
            basics();
        }
    }

    /**
     * 基础作业的加密方法
     * @param psw 用户输入的密码
     * @return  加密后的密码
     */
    public static String method1(String psw) {
        //对于每个字母和数字, 按字母表/数字顺序往后挪5位, 符号不变
        //如密码: AaZz09!!!
        //加密后: FfEe54!!!
        char[] ch = psw.toCharArray();  //将字符串转成字符数组
        char temp = ' ';    //定义临时变量
        for(int i = 0; i < ch.length; i++){     //遍历
            temp = ch[i];   //赋值给临时变量
            if (65 <= temp && temp <= 90){                  //大写字母 A-Z:65-90,
                ch[i] = temp > 85 ? temp -= 21 : (temp += 5);
            }else if(97 <= temp && temp <= 122) {           //小写字母 a-z:97-122,
                ch[i] = temp > 117 ? temp -= 21 : (temp += 5);
            }else if (48 <= temp && temp <= 57){            //数字 0-9:48-57
                ch[i] = temp > 52 ? temp -= 5 : (temp += 5);
            }else {
                ch[i] = ch[i];  //符号不变
            }
        }
        String encryption = String.valueOf(ch); //转为字符串
        return encryption;
    }

//**********************************************************************************************************************************

    /**
     * 是否继续计算
     */
    public static boolean judge() {
        System.out.print("\n是否继续?输入y继续计算,其他则退出: ");
        Scanner sc = new Scanner(System.in);
        if ("y".equalsIgnoreCase(sc.next())){
            System.out.print("继续判断,");
            return true;
        }else{
            System.out.println("退出计算!");
            return false;
        }
    }

    /**
     * 用户输入
     * @return 返回非空字符串
     */
    public static String input() {
        Scanner sc = null;
        String psw = null;
        while(true){
            sc = new Scanner(System.in);
            System.out.print("请输入密码:");
            psw = sc.nextLine().trim();     //去掉首尾空格
            if (psw.length() != 0){     //判断是否非空
                return psw;
            }else{
                System.out.print("请输入非空密码: ");
            }
        }
    }

}

运行结果

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 1

******************标准作业******************
请输入密码:AaZz09!!!

初始密码: AaZz09!!!		加密后为: FfEe54!!!

是否继续?输入y继续计算,其他则退出: n
退出计算!

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 2

******************进阶作业******************
请输入密码:AaZz09!!!

初始密码: AaZz09!!!		加密后为: BbZz01!!!

是否继续?输入y继续计算,其他则退出: n
退出计算!

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 3

******************系统退出******************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值