牛客网力扣算法编程之五 | 字符串- 简单密码-Java代码实现

【算法编程】- 简单密码

一. 题目描述
        密码是我们生活中非常重要的东东,我们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。

        假设渊子原来一个BBS上的密码为zvbo9441987,为了方便记忆,他通过一种算法把这个密码变换成YUANzhi1987,这个密码是他的名字和出生年份,怎么忘都忘不了,而且可以明目张胆地放在显眼的地方而不被别人知道真正的密码。

       他是这么变换的,大家都知道手机上的字母: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,就这么简单,渊子把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,


 声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,不就是 y 了嘛,简单吧。记住,Z 往后移是 a 哦。

数据范围: 输入的字符串长度满足1≤n≤100

本题有多组样例输入
输入描述:
输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾

输出描述:
输出渊子真正的密文

示例1
输入:
YUANzhi1987
输出:
zvbo9441987

二. 解题思路总结:

1.处理多组输入时,在外层加上while(sc.hasNextLine());

2.逐个判断字符并转换:如果字符是A-Y,这个字符++,通过toLowerCase变换成小写;

3.如果是Z,就变成'a';

4.如果是a-z字符,按照手机键盘转换小写字母成对应数字,添加到输出res后;

5.其余情况,直接加到res后,打印输出字符串res。

三. Java代码如下:

import java.util.*;
public class Main {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =new String();
        str="";
        String res =new String();
        res="";
        while(sc.hasNextLine()) {    //处理多组输入
            str = sc.nextLine();
            if (str != "") {
                for (int i = 0; i < str.length(); i++) {
                    if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Y') {   //如果字符是A-Y,这个字符++
                        char midd = str.charAt(i);
                        midd++;
                        String mid = (midd + "").toLowerCase();   //通过toLowerCase变小写,输出res里
                        res = res + mid;
                    } else if (str.charAt(i) == 'Z') {   //如果是Z,就编程'a'
                        char midd = 'a';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'a' || str.charAt(i) == 'b' || str.charAt(i) == 'c') {
                        char midd = '2';              //按照手机键盘转换小写字母成对应数字
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'd' || str.charAt(i) == 'e' || str.charAt(i) == 'f') {
                        char midd = '3';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'g' || str.charAt(i) == 'h' || str.charAt(i) == 'i') {
                        char midd = '4';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'j' || str.charAt(i) == 'k' || str.charAt(i) == 'l') {
                        char midd = '5';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'm' || str.charAt(i) == 'n' || str.charAt(i) == 'o') {
                        char midd = '6';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'p' || str.charAt(i) == 'q' || str.charAt(i) == 'r' || str.charAt(i) == 's') {
                        char midd = '7';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 't' || str.charAt(i) == 'u' || str.charAt(i) == 'v') {
                        char midd = '8';
                        res = res + midd + "";
                    } else if (str.charAt(i) == 'w' || str.charAt(i) == 'x' || str.charAt(i) == 'y' || str.charAt(i) == 'z') {
                        char midd = '9';
                        res = res + midd + "";   //添加到输出res后
                    } else {
                        char midd = str.charAt(i);    //其余情况,直接加到res后
                        res = res + midd + "";
                    }
                }
                System.out.println(res);   //输出res字符串

            }
        }
    }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国林哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值