将数字转换为单词

-6 java jgrasp

我想得到一些关于将整数转换为单词的帮助.我是Java的新手,我有点想到我将要做的事情,但需要一些帮助.

代码应打印0-999的数字,并在扫描仪中输入-1,程序应停止.

像这样:

请输入0到999之间的数字:1

请输入0到999之间的数字:11

十一

请输入0到999之间的数字:122

一百二十二

请输入0到999之间的数字:1000

数量超出范围

请输入0到999之间的数字:-1

感谢您使用我们的计划

我还必须使用方法来使这个"清洁"

kri*_*eta 5

首先,通过调用100来取数百位数并通过调用方法打印相应的数字, numberToWord((number / 100), " HUNDRED");因为数字/ 100将在0到9之间,因此它将在由HUNDRED连接的单词中打印数字.

现在你留下了两个数字,你直接打电话,numberToWord((number % 100), " ");因为我们采用模数100的模数,所以它只传递两位数.if (num > 19) {System.out.print(tens[num / 10] + " " + ones[num % 10]);}然后它将需要几十个位置并打印十个单词连接的单词.else {System.out.print(ones[num]);}直接从数组中打印1到19之间的单词.

import java.util.Scanner;

public class Test1 {



        public static void main(String[] args) {
            int number = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
            number = scanner.nextInt();
            while(number!=-1){
                if(number>=0 && number<=999){
                    if(number==0){
                        System.out.print("NUMBER AFTER CONVERSION:\tZERO");
                    } else {
                        System.out.print("NUMBER AFTER CONVERSION:\t");
                        numberToWord(((number / 100) % 10), " HUNDRED");
                        numberToWord((number % 100), " ");
                    }

                } else{
                    System.out.print("NUMBER OUT OF RANGE");
                }
                System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit:  ");
                number = scanner.nextInt();
            }
        }

        public static void numberToWord(int num, String val) {
            String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
            };
            String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
            if (num > 19) {
                System.out.print(tens[num / 10] + " " + ones[num % 10]);
            } else {
                System.out.print(ones[num]);
            }
            if (num > 0) {
                System.out.print(val);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

样本输出:

Please type a number between 0 and 999 OR type -1 to exit:  563
NUMBER AFTER CONVERSION:     FIVE HUNDRED SIXTY  THREE 
Please type a number between 0 and 999 OR type -1 to exit:  45
NUMBER AFTER CONVERSION:      FOURTY  FIVE 
Please type a number between 0 and 999 OR type -1 to exit:  6 
NUMBER AFTER CONVERSION:      SIX 
Please type a number between 0 and 999 OR type -1 to exit:  0
NUMBER AFTER CONVERSION:    ZERO 
Please type a number between 0 and 999 OR type -1 to exit:  -1
exit
Run Code Online (Sandbox Code Playgroud)

  • @PaulRichter 在下次回答之前,我会牢记您的话。谢谢 (2认同)

Roh*_*ain 5

这是 Number to Word 程序的递归解决方案

 string unitsMap[] = { "zero", "one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tensMap[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

string NumberToWords(int number)
{
 if (number == 0)
    return "zero";

if (number < 0)
    return "minus " + NumberToWords(abs(number));

string words = "";

if ((number / 1000000000) > 0)
{
    words += NumberToWords(number / 1000000000) + " billion ";
    number %= 1000000000;
}

if ((number / 1000000) > 0)
{
    words += NumberToWords(number / 1000000) + " million ";
    number %= 1000000;
}

if ((number / 1000) > 0)
{
    words += NumberToWords(number / 1000) + " thousand ";
    number %= 1000;
}

if ((number / 100) > 0)
{
    words += NumberToWords(number / 100) + " hundred ";
    number %= 100;
}

if (number > 0)
{
    if (number < 20)
        words += unitsMap[number];
    else
    {
        words += tensMap[number / 10];
        if ((number % 10) > 0)
            words += "-" + unitsMap[number % 10];
    }
}

return words;
 }
Run Code Online (Sandbox Code Playgroud)