简体   繁体   English

在Java中将数字转换为单词。 有没有更简单,更有效的方法呢?

[英]Converting number to words in Java. Is there an easier and more efficient way to do it?

Hi guys i'm trying to write a simple program using control structures to convert numbers to words but the program is becoming way too long. 嗨,大家好,我正在尝试使用控制结构编写一个简单的程序,以将数字转换为单词,但是该程序变得太长了。 Is there a simpler way to write it? 有没有更简单的编写方法? An example is, if a user inputs 123 the output should be one two three. 例如,如果用户输入123,则输出应为一二三。 I didn't complete it but here is a sample: 我没有完成它,但是这里有一个示例:

import java.util.Scanner;
public class Number10 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String num;

    System.out.print("Enter a number and i'll convert it to words: ");
    num = input.nextLine();

    if((num.length()) == 1)
    {
        switch(num)
        {
        case "0":
        {
            System.out.print("Zero");
            break;
        }
        case "1":
        {
            System.out.print("One");
            break;
        }
        case "2":
        {
            System.out.print("Two");
            break;
        }
        case "3":
        {
            System.out.print("Three");
            break;
        }
        case "4":
        {
            System.out.print("Four");
            break;
        }
        case "5":
        {
            System.out.print("Five");
            break;
        }
        case "6":
        {
            System.out.print("Six");
            break;
        }
        case "7":
        {
            System.out.print("Seven");
            break;
        }
        case "8":
        {
            System.out.print("Eight");
            break;
        }
        case "9":
        {
            System.out.print("Nine");
            break;
        }
        default:
        {
            System.out.print("Please enter a number");
            break;
        }
        }
 }
    else if((num.length()) == 2)
    {
        switch(num)
        {
        case "11":
        {
            System.out.print("One One");
            break;
        }
        case "12":
        {
            System.out.print("One Two");
            break;
        }
        case "13":
        {
            System.out.print("One Three");
            break;
        }
        case "14":
        {
            System.out.print("One Four");
            break;
        }
        case "15":
        {
            System.out.print("One Five");
            break;
        }
        case "16":
        {
            System.out.print("One Six");
            break;
        }
        case "17":
        {
            System.out.print("One Seven");
            break;
        }
        case "18":
        {
            System.out.print("One Eight");
            break;
        }
        case "19":
        {
            System.out.print("One Nine");
            break;
        }
        case "20":
        {
            System.out.print("Two Zero");
            break;
        }
        case "21":
        {
            System.out.print("Two One");
            break;
        }
        case "22":
        {
            System.out.print("Two Two");
            break;
        }
        case "23":
        {
            System.out.print("Two Three");
            break;
        }
        case "24":
        {
            System.out.print("Two Four");
            break;
        }

        default:
        {
            System.out.print("Please enter a number");
            break;
        }
        }
    }

    else
    {
        System.out.print("Invalid number");
    }

}

} }

Please help me out. 请帮帮我。 Thanks. 谢谢。

I would do this: 我会这样做:

import java.util.Scanner;
public class Number10 {

private static String[] nums = new String[]
        {"Zero", "One", "Two", "Three", "Four",
         "Five", "Six", "Seven", "Eight", "Nine"};

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String num;

    System.out.print("Enter a number and i'll convert it to words: ");
    num = input.nextLine();
    input.close()

    for (Character c: num.toCharArray())
        System.out.print(nums[Character.getNumericValue(c)]+ ' ');

}

First of all, you could create a 首先,您可以创建一个

java.util.Map<Integer, String>

holding a mapping of integer values to string representations, but this will only shorten your code. 保留整数值到字符串表示形式的映射,但这只会缩短您的代码。 The next step would be to handle numbers by digits, so you might hold a map of digit to string representation and print the string representation for each digit: 下一步是按数字处理数字,因此您可以持有一个数字映射到字符串表示形式,并为每个数字打印字符串表示形式:

Map<Integer, String> numbers = new HashMap<Integer, String>();
numbers.put(0, "Zero");
// 22 = numbers.get(2) + " " + numbers.get(2);
// 51 = numbers.get(5) + " " + numbers.get(1);
// ...

It's much better to do this with a HashMap<Integer,String> . 最好使用HashMap<Integer,String> This makes for cleaner code and for more flexibility. 这使得代码更简洁,灵活性更高。 If, for instance, you want to add functionality for other languages as well. 例如,如果您也想添加其他语言的功能。

Use a loop to fill the HashMap with .put() , perhaps from a file. 使用循环将.put()填充到HashMap中,也许是从文件中填充。 Then use .get() to retrieve the translations. 然后使用.get()检索翻译。

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

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