题解 | #简单密码#

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
            String strup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String[] change = new String[str.length()];
        for (int i = 0; i < str.length(); i++) {
            change[i] = str.substring(i,i+1);
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < change.length; i++) {
            //小写
            if (!strup.contains(change[i])){
                sb.append(low2num(change[i]));
            }else {
                //大写
                sb.append(up2low(change[i]));
            }
        }
        System.out.println(sb.toString());

    }

    //小写字母变数字
    public static String low2num(String low){
        String res = "";
        switch (low){
            case "a":
            case "b":
            case "c":
                res = "2";
                break;
            case "d":
            case "e":
            case "f":
                res = "3";
                break;
            case "g":
            case "h":
            case "i":
                res = "4";
                break;
            case "j":
            case "k":
            case "l":
                res = "5";
                break;
            case "m":
            case "n":
            case "o":
                res = "6";
                break;
            case "p":
            case "q":
            case "r":
            case "s":
                res = "7";
                break;
            case "t":
            case "u":
            case "v":
                res = "8";
                break;
            case "w":
            case "x":
            case "y":
            case "z":
                res = "9";
                break;
            default:
                res = low;
                break;
        }

        return res;
    }

    //大写字母返回小写字母的下一个字母
    public static String up2low(String le){
        String res = "";
        String[] low = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        String strup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int index = strup.indexOf(le);
        if (index==25){
            res = low[0];
        }else {
            res = low[index+1];
        }

        return res;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务