【问题标题】:Numbers to letters in java (like old mobile phones' keyboard)Java中的数字到字母(如旧手机的键盘)
【发布时间】:2015-06-03 17:53:18
【问题描述】:

就像旧手机的键盘一样。我应该输入一串数字,然后程序应该根据这些数字打印出一个文本。

例如:输入:4448 9666777557777 应输出到:ITWORKS。

到目前为止,这是我的代码,但它没有打印出任何内容。你能告诉我它有什么问题吗?我可以做得更好吗?

    Scanner sc = new Scanner(System.in);

    String[] letters = {
            "0",
            "1",
            "ABC",
            "DEF",
            "GHI",
            "JKL",
            "MNO",
            "PQRS",
            "TUV",
            "WXYZ"
    };

    System.out.println("Write something.");
    String numbers = sc.nextLine();

    char[] toChar = numbers.toCharArray();
    int count = 0;

    for (int index = 0; index < toChar.length; index++) {
        if (toChar[index] >= '2' && toChar[index] <= '9') {
            if (index > 0 && toChar[index] == toChar[index - 1]) {
                count++;
            }
            else if (count > 0) {
                System.out.print(letters[toChar[index - 1] - '0'].charAt(count - 1));
                count = 0;              
            }
        }
    }

【问题讨论】:

  • 想想如果一个电话号码有两个相同的数字一个接一个会发生什么。例如应该将7777 投影到一个S (7777) 或两个Qs (77 + 77),或者可能是PR (7 + 777)...
  • 我很确定 (7777) 应该总是等于 S,(77777) 等于 SP,(777777) 等于 SQ,等等。
  • 使用这种编码方式是无法得到QRPS的(因为它们都将被编码为77777并解码为SQ
  • 哦,是的。我明白。嗯...当我现在考虑它时,77777 应该等于 P - 也就是围绕 4 个字母旋转,直到看到一个数字!= 7。
  • 你看到我的回答了吗?它解决了所有问题。

标签: java string numbers letters


【解决方案1】:

这个怎么样?

import java.util.Scanner;

public class Test {
    private static final String[] letters = {
            "0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"
    };

    private static char getChar(int digit, int count) {
        while (count > letters[digit].length()) {
            count -= letters[digit].length();
        }

        return letters[digit].charAt(count - 1);
    }

    private static String getString(String input) {
        int lastDigit = 0, count = 1;
        String result = "";

        for (int i = 0; i < input.length(); i++) {
            int currentDigit = input.charAt(i) - '0';
            if (currentDigit >= 2 && currentDigit <= 9) {
                if (lastDigit == 0) {
                    lastDigit = currentDigit;
                } else if (currentDigit == lastDigit) {
                    count++;
                } else {
                    result += getChar(lastDigit, count);

                    lastDigit = currentDigit;
                    count = 1;
                }
            }
        }

        return result + getChar(lastDigit, count);
    }

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Write something");
            System.out.println(getString(scanner.nextLine()));
        }
    }
}

我增强了问题分解。它适用于 OP 迄今为止展示的所有示例。

【讨论】:

  • 谢谢!接受了你的回答。它与我的想法相似,但完美无缺!你能解释一下为什么你最后返回result + getChar(lastDigit, count)而不是简单的result吗?
  • 好吧,对于输入的最后一个序列,它没有达到该条件的“其他”部分。代码,它是怎样的,基本上对变化“做出反应”——一个新的数字(例如:“777|8...” - 8 是变化)。对于输入的最后一个序列,不会发生这种变化。可以更改代码,以便在更改或字符串结尾时评估“else”部分。
  • 顺便问一下,你知道我怎么可能在上述代码中引入间隔''作为分隔符吗?
【解决方案2】:

如果我正确理解您的意图,count 只有在当前数字与前一个数字相同时才会增加:

for (int pos = 1, char c = toChar[0], int count = 1; pos <= toChar.length; pos++, count = 1) {

   int n = letters[c - '0'].length;
   while (pos < toChar.length && c == toChar[pos] && count < n) {
       pos++;
       count++;
   }
   System.out.println(letters[c - '0'].charAt(count - 1));
   if (pos < toChar.length - 1) {
       c = toChar[++pos];
   }
}

【讨论】:

  • 看起来不错,但对于2,它不会打印任何内容。 22 工作正常,输出 B,但 2233 输出 BD 而不是 BE
  • 忘记在 for 循环中重新分配 c
猜你喜欢
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多