九宫格输入法(最易懂版本)

P2 九宫格输入法 (15point(s))

假设有九宫格输入法键盘布局如下:

 [ 1,.?! ] [ 2ABC ] [ 3DEF  ]
 [ 4GHI  ] [ 5JKL ] [ 6MNO  ]
 [ 7PQRS ] [ 8TUV ] [ 9WXYZ ]
           [ 0]

注意:中括号[ ]仅为了表示键盘的分隔,不是输入字符。每个中括号中,位于首位的数字字符即是键盘的按键,按一下即可输入该数字字符。多次按同一个键,则输入的字符依次循环轮流,例如按两次3,则输入D;按5次7,则输入S;按6次2,则输入A。按键0的输入组合是0和空格字符,即按两次0输入空格。

你需要对于给定的按键组合,给出该组合对应的文本。

输入格式:

输入在一行中给出数个字符的按键组合(例如 999 表示按3次9),每个字符的按键组合之间用空格间隔,最后一个输入法组合之后以换行结束。输入数据至少包括一个字符的按键组合,且输入总长度不超过500个字符。

输出格式:

在一行中输出该按键组合对应的文本。

输入样例:

22 5555 22 666 00 88 888 7777 4444 666 44

输出样例:

ALAN TURING
[解]:直接简单粗暴,根据题意采取循环计次输入策略。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str= scanner.nextLine();//读入一行字符串
        String[] data =str.split(" ");//按空格分开并存入数组
        for (String datum : data) {//将数组元素长度作为循环次数,数组内容作为输入数据,根据输入内容变换输出内容
            if (datum.charAt(0) == '1') {
                if (datum.length() % 5 == 1) {
                    System.out.print("1");
                } else if (datum.length() % 5 == 2) {
                    System.out.print(",");
                } else if (datum.length() % 5 == 3) {
                    System.out.print(".");
                } else if (datum.length() % 5 == 4) {
                    System.out.print("?");
                } else {
                    System.out.print("!");
                }
            } else if (datum.charAt(0) == '2') {
                if (datum.length() % 4 == 1) {
                    System.out.print("2");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("A");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("B");
                } else {
                    System.out.print("C");
                }
            } else if (datum.charAt(0) == '3') {
                if (datum.length() % 4 == 1) {
                    System.out.print("3");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("D");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("E");
                } else {
                    System.out.print("F");
                }
            } else if (datum.charAt(0) == '4') {
                if (datum.length() % 4 == 1) {
                    System.out.print("4");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("G");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("H");
                } else {
                    System.out.print("I");
                }
            } else if (datum.charAt(0) == '5') {
                if (datum.length() % 4 == 1) {
                    System.out.print("5");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("J");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("K");
                } else {
                    System.out.print("L");
                }
            } else if (datum.charAt(0) == '6') {
                if (datum.length() % 4 == 1) {
                    System.out.print("6");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("M");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("N");
                } else {
                    System.out.print("O");
                }
            } else if (datum.charAt(0) == '7') {
                if (datum.length() % 5 == 1) {
                    System.out.print("7");
                } else if (datum.length() % 5 == 2) {
                    System.out.print("P");
                } else if (datum.length() % 5 == 3) {
                    System.out.print("Q");
                } else if (datum.length() % 5 == 4) {
                    System.out.print("R");
                } else {
                    System.out.print("S");
                }
            } else if (datum.charAt(0) == '8') {
                if (datum.length() % 4 == 1) {
                    System.out.print("8");
                } else if (datum.length() % 4 == 2) {
                    System.out.print("T");
                } else if (datum.length() % 4 == 3) {
                    System.out.print("U");
                } else {
                    System.out.print("V");
                }
            } else if (datum.charAt(0) == '9') {
                if (datum.length() % 5 == 1) {
                    System.out.print("9");
                } else if (datum.length() % 5 == 2) {
                    System.out.print("W");
                } else if (datum.length() % 5 == 3) {
                    System.out.print("X");
                } else if (datum.length() % 5 == 4) {
                    System.out.print("Y");
                } else {
                    System.out.print("Z");
                }
            } else if (datum.charAt(0) == '0') {
                if (datum.length() % 2 == 1) {
                    System.out.print("0");
                } else {
                    System.out.print(" ");
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

量子代码时空

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

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

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

打赏作者

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

抵扣说明:

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

余额充值