简体   繁体   English

java 中的数字到字母(如旧手机的键盘)

[英]Numbers to letters in java (like old mobile phones' keyboard)

Just like older mobile phones' keypads work.就像旧手机的键盘一样。 I should input a string of numbers and the program should print out a text based on those numbers.我应该输入一串数字,程序应该根据这些数字打印出一个文本。

eg: Input: 4448 9666777557777 should output to: ITWORKS.例如:输入:4448 9666777557777 应 output 至:ITWORKS。

Here's my code so far but it's not printing out anything.到目前为止,这是我的代码,但它没有打印出任何东西。 Could you please tell me what's wrong with it and what could've I done better?你能告诉我它有什么问题吗?我可以做得更好吗?

    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;              
            }
        }
    }

If I understand your intention correctly, count should increment only if current digit is the same as previous: 如果我正确理解您的意图,则仅当当前数字与以前的数字相同时, 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];
   }
}

How about this? 这个怎么样?

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()));
        }
    }
}

I enhanced the problem decomposition. 我加强了问题分解。 It works for all examples OP has shown so far. 它适用于OP到目前为止显示的所有示例。

This code will help you!这段代码可以帮助你! Check this one检查这个

public class Denene {
    public static String getChar(String cha)
    {
        String [] chars= {"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        String[] strSplit = cha.split(" "); //7777 88 7777 2 66 8
        int len=strSplit.length;
        char r;
        char []ar =new char[len];
      for(int i=0;i<len;i++){
          String str=strSplit[i];
          ar[i]= chars[Integer.parseInt(String.valueOf(str.charAt(0)))].charAt(str.length()-1);
        }
        return new String(ar);
    }
        public static void main(String[] args) {
            System.out.println("Enter any number .....");
           System.out.println(getChar(new Scanner(System.in).nextLine())); //Output : susant
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM