-5

我想在将整数转换为单词方面获得一些帮助。我对 Java 很陌生,我对我认为我将要做什么有所了解,但需要一些帮助。

代码应打印 0-999 之间的数字单词,一旦在扫描仪中键入 -1,程序应停止。

像这样:

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

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

十一

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

一百二十二

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

数量超出范围

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

感谢您使用我们的程序

我还必须使用方法使这个“更清洁”

4

9 回答 9

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

样本输出:

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
于 2014-11-17T17:37:39.960 回答
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;
 }
于 2016-02-04T11:52:23.383 回答
4
public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    };

    private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
    };

    private String convertLessThanOneThousand(int number) {
        String current;

        if (number % 100 < 20){
            current = numNames[number % 100];
            number /= 100;
        }
        else {
            current = numNames[number % 10];
            number /= 10;

            current = tensNames[number % 10] + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred" + current;
    }

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

    public static void main(String[] args) {
        NumberToWord obj = new NumberToWord();
        System.out.println("*** " + obj.convert(123456789));
        System.out.println("*** " + obj.convert(-55));
    }
}

资料来源:http: //javahungry.blogspot.com/2014/05/convert-math-number-to-equivalent-readable-word-in-java-code-with-example.html

于 2017-04-07T09:58:43.827 回答
1

试试这个:

public class WordToNumber {

static String string;
static String st1[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
static int st2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

public static int mac(String n) {
    int digit = 0;
    try {
        if(Arrays.asList(st1).contains(n.toLowerCase())){
            int a = Arrays.asList(st1).indexOf(n.toLowerCase());
            digit = st2[a];
        }
        else{
            return -1;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    return digit;
}

}

于 2018-02-27T07:15:58.350 回答
1

//此代码可以打印从0到Crore的数字到单词

公共类 NumericToWords {

static Map<Long, String> map1 = new HashMap<Long, String>();

public static void main(String[] args) {
    Long num = new Long(300001);
    print(num, num.toString().length());
}

private static void print(Long num, int length) {
    if (length == 0) {
        System.out.println("Enter correct number");
    } else if (length == 1 || num <= 9) {
        printOneDigit(num);
    } else if (length == 2 || num <= 99) {
        printTwoDigit(num);
    } else if (length == 3 || num <= 999) {
        printThreeDigit(num);
    } else if (length == 4 || num <= 9999) {
        printFourDigit(num);
    } else if (length == 5 && num <= 99999) {
        printFiveDigit(num);
    } else if (length == 6 || num <= 999999) {
        printSixDigit(num);
    } else if (length == 7 || num <= 9999999) {
        printSevenDigit(num);
    } else if (length == 7 || num <= 99999999) {
        printEightDigit(num);
    } else
        System.out.println("Number is not valid....!!!!!");
}

private static void printOneDigit(Long num) {
    System.out.print(map1.get(num) != null ? map1.get(num) : "" + " ");
}

private static void printTwoDigit(Long num) {
    if (num % 10 == 0)
        System.out.print(map1.get(num) + "");
    else if (num < 20) {
        System.out.print(map1.get(num) + " ");
    } else {
        System.out.print(map1.get(num - num % 10) != null ? map1.get(num - num % 10) : "");
        System.out.print(" ");
        printOneDigit(num % 10);
    }
}

private static void printThreeDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0)
        System.out.print(map1.get(num / 100) + " HUNDRED");
    else {
        System.out.print(map1.get(num / 100) + " HUNDRED ");
        print(num % 100, 2);
    }
}

private static void printFourDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0 && num % 1000 == 0)
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
    else {
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
        print(num % 1000, 3);
    }
}

private static void printFiveDigit(Long num) {
    if (num / 1000 > 0) {
        printTwoDigit(num / 1000);
        System.out.print(" THOUSAND ");
    }
    if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printSixDigit(Long num) {
    if (num / 100000 > 0) {
        printOneDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num > 0)
        printFiveDigit(num % 100000);
}

private static void printSevenDigit(Long num) {
    if (num / 100000 > 0) {
        printTwoDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num % 100000 > 9999) {
        printFiveDigit(num % 100000);
    } else if ((num % 10000) > 999) {
        printFourDigit(num % 10000);
    } else if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printEightDigit(Long num) {
    printOneDigit(num / 10000000);
    System.out.print(" CRORE ");
    if (num > 0)
        printSevenDigit(num % 10000000);
}

static {
    map1.put(0L, "");
    map1.put(1L, "ONE");
    map1.put(2L, "TWO");
    map1.put(3L, "THREE");
    map1.put(4L, "FOUR");
    map1.put(5L, "FIVE");
    map1.put(6L, "SIX");
    map1.put(7L, "SEVEN");
    map1.put(8L, "EIGHT");
    map1.put(9L, "NINE");
    map1.put(10L, "TEN");
    map1.put(11L, "ELEVEN");
    map1.put(12L, "TWELVE");
    map1.put(13L, "THIRTEEN");
    map1.put(14L, "FOURTEEN");
    map1.put(15L, "FIFTEEN");
    map1.put(16L, "SIXTEEN");
    map1.put(17L, "SEVENTEEN");
    map1.put(18L, "EIGHTEEN");
    map1.put(19L, "NINTEEN");
    map1.put(20L, "TWINITY");
    map1.put(30L, "THIRTY");
    map1.put(40L, "FOURTY");
    map1.put(50L, "FIFTY");
    map1.put(60L, "SIXTY");
    map1.put(70L, "SEVENTY");
    map1.put(80L, "EIGHTY");
    map1.put(90L, "NINTY");
}

}

于 2018-06-21T10:34:25.603 回答
0

这是你如何做到的。我将它从 1 扩展到 10,但您可以将其从 1 扩展到 999。

   //     values entered        |     result
   //     2                     |     You have entered:Two
   //     10                    |     You have entered: Ten
   //     23                    |     You have entered: 23 which is not in range.

int i = Integer.parseInt(txtInput.getText());
    String[] namesOfNums = {"One", "Two", "Three","Four", "Five", "Six", "Seven","Eight", "Nine","Ten" };
    String output = "";
    if (i > 0 && i <=10) 
    {

        output = namesOfNums[i - 1];
        lblDisplay.setText("You have entered: "+ output);

    }
    else
    {
      lblDisplay.setText("You have entered: "+ i +" which is not range.");
    }
于 2017-02-26T15:54:46.637 回答
0
import java.util.Scanner;
class Name
{
    static String string;
    static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
    static String st2[]={"hundred","thousand","lakh","crore"};
    static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        System.out.println(mac(in.nextInt())+" only");
    }
    public static String mac(int n)
    {
        int word;
        int nu=1;
        string="";
        while(n!=0)
        {
            switch(nu)
            {
            case 1:
                word=n%100;
                pass(word);
                if(n>100&&n%100!=0)
                {
                    show("and ");
                }
                n=n/100;
                break;
            case 2:
                word=n%10;
                if(word!=0)
                {
                    show(" ");
                    show(st2[0]);
                    show(" ");
                    pass(word);
                }
                n=n/10;
                break;
            case 3:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[1]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 4:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[2]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 5:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[3]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            }
            nu++;
        }
        return string;
    }
    public static void pass(int n)
    {
        int word,q;
        if(n<10)
        {
            show(st1[n]);
        }
        if(n>9&&n<20)
        {
            show(st3[n-10]);
        }
        if(n>19)
        {
            word=n%10;
            if(word==0)
            {
                q=n/10;
                show(st4[q-2]);
            }
            else
            {
                q=n/10;
                show(st1[word]);
                show(" ");
                show(st4[q-2]);
            }
        }
    }
    public static void show(String s)
    {
        String st=string;
        string=s+st;
    }
}
于 2017-08-10T01:25:49.773 回答
0
import java.util.Scanner;

public class number to words  {

  static String string;
  static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
  static String st2[]={"hundred","thousand","lakh","crore"};
  static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
  static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

  public static void main(String args[])
  {
    Scanner in=new Scanner(System.in);
    System.out.println("ENTER THE NUMBER");
    System.out.println(mac(in.nextInt())+" only");
  }
  public static String mac(int n)
  {
    int word;
    int nu=1;
    string="";
    while(n!=0)
    {
        switch(nu)
        {
        case 1:
            word=n%100;
            pass(word);
            if(n>100&&n%100!=0)
            {
                show("and ");
            }
            n=n/100;
            break;
        case 2:
            word=n%10;
            if(word!=0)
            {
                show(" ");
                show(st2[0]);
                show(" ");
                pass(word);
            }
            n=n/10;
            break;
        case 3:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[1]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 4:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[2]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 5:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[3]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        }
        nu++;
    }
    return string;
  }
  public static void pass(int n)
  {
    int word,q;
    if(n<10)
    {
        show(st1[n]);
    }
    if(n>9&&n<20)
    {
        show(st3[n-10]);
    }
    if(n>19)
    {
        word=n%10;
        if(word==0)
        {
            q=n/10;
            show(st4[q-2]);
        }
        else
        {
            q=n/10;
            show(st1[word]);
            show(" ");
            show(st4[q-2]);
        }
    }
  }
  public static void show(String s)
  {
    String st=string;
    string=s+st;
  }
}
于 2017-09-28T09:22:23.120 回答
0

该解决方案最多支持 66 位数字,并且可以根据不同的计数系统进行修改。要了解此计数系统,请参阅https://ptdeepali.wordpress.com/2013/10/28/vedic-numbering-system/

package com.number_to_word;

import java.util.Scanner;

/**
 * This program is to get the word value of given number. 
 * <p>Number can be ranges from 0 to 66th power of 10 (66 digits).</p>
 * Number can be passed through Scanner and it will print the word value of that number.
 * 
 * @author DHARAMRAJSINGH
 * 
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter number: ");
        String n = sc.nextLine();
        System.out.println(getWord(n));
        sc.close();
    }

    /**
     * This method is using array inside it and this array is 2D array contains word values.
     * <p>This array ranges from arr[0][0] to arr[9][9]. </p>
     * <p>The array is initialized in a way that it will return values like, for arr[0][0] returns zero 
     * </br> arr[0][1] returns one, arr[2][1] returns twenty one, arr[9][5] returns ninety five, and so on. </p>
     * The logic is the number <strong>n</strong> is passed into it and divided by 10, the quotient will be index0 and remainder will be index1,
     * and return arr[index0][index1].
     * </br>  
     * @param n will be in the range of 0 to 99
     * @return the word value ranges from 0 to 99
     */
    private static String getValue(int n) {
        int index0 = 0, index1 = 0;
        if(n < 0 || n > 99) {
            throw new IllegalArgumentException("number cannot be less than zero or greater than 99");
        }
        if(n < 10) {
            index1 = n;
        } else {
            index1 = n%10;
            index0 = n/10;
        }
        String arr [][] = {
                // for "zero", "one" .... "nine"
                {"शुन्य",   "एकः",  "द्वौ", "त्रयः",    "चत्वारः",  "पञ्च", "षट्",  "सप्त", "अष्ट", "नव"},
                // for "ten", "eleven" .... "nineteen"
                {"दश",  "एकादशम्",  "द्वादशम्", "त्रयोदशम्",    "चतुर्दशम्",    "पञ्चदशम्", "षोडशम्",   "सप्तदशम्", "अष्टादशम्", "नवदशम्"},
                // for "twenty", "twenty one" .... "twenty nine"
                {"विंशति",  "एकाविंशति",    "द्वाविंशति",   "त्रयोविंशति",  "चतुर्विंशति",  "पञ्चविंशति",   "षड्विंशति",    "सप्तविंशति",   "अष्टाविंशति", "नवविंशति"},
                // for "thirty", "twenty one" .... "thirty nine"
                {"त्रिंशत्",    "एकत्रिंशत्",   "द्वात्रिंशत्", "त्रयत्रिंशत्", "चतुस्त्रिंशत्",    "पञ्चत्रिंशत्", "षट्त्रिंशत्",  "सप्तत्रिंशत्", "अष्टात्रिंशत्", "एकोनचत्वारिंशत्"},
                {"चत्वारिंशत्", "एकचत्वारिंशत्",    "द्विचत्वारिंशत्",  "त्रिचत्वारिंशत्",  "चतुश्चत्वारिंशत्", "पञ्चचत्वारिंशत्",  "षट्चत्वारिंशत्", "सप्तचत्वारिंशत्",    "अष्टचत्वारिंशत्",  "एकोनपञ्चाशत्"},
                {"पञ्चाशत्",    "एकपञ्चाशत्",   "द्विपञ्चाशत्", "त्रिपञ्चाशत्", "चतुःपञ्चाशत्", "पञ्चपञ्चाशत्", "षट्पञ्चाशत्",  "सप्तपञ्चाशत्", "अष्टपञ्चाशत्","एकोनषष्ठिः"},
                {"षष्ठिः",      "एकषष्ठिः", "द्विषष्ठिः",   "त्रिषष्ठिः",   "चतुःषष्ठिः",   "पञ्चषष्ठिः",   "षट्षष्ठिः",    "सप्तषष्ठिः",   "अष्टषष्ठिः",   "एकोनसप्ततिः"},
                {"सप्ततिः", "एकसप्ततिः",    "द्विसप्ततिः",  "त्रिसप्ततिः",  "चतुःसप्ततिः",  "पञ्चसप्ततिः",  "षट्सप्ततिः",   "सप्तसप्ततिः","अष्टसप्ततिः","एकोनाशीतिः"},
                {"अशीतिः",  "एकाशीतिः", "द्वशीतिः", "त्र्यशीतिः",   "चतुरशीतिः",    "पञ्चाशीतिः",   "षडशीतिः",  "सप्ताशीतिः",   "अष्टाशीतिः","एकोननवतिः"},
                // for "ninety", "two" .... "ninety nine"
                {"नवतिः",   "एकनवतिः",  "द्विनवतिः",    "त्रिनवतिः",    "चतुर्नवतिः",   "पञ्चनवतिः",    "षण्णवतिः", "सप्तनवतिः",    "अष्टनवतिः","एकोनशतम्"}

        };
        return arr[index0][index1];
    }
    /**
     * This method returns the place value of the digit, like for index value 2 it will return hundred, 
     * for index value 3 and 4 it will thousand, for index value 5 and 6 it will lacs, and so on. 
     * <p>It return empty string for index 0 and 1 as it does not have specific name for that. 
     * @param n is index value of digit
     * @return place value of index (position like unit place, tens place etc.)
     */
    private static String getPlaceValue(int n) {
        String [] arr = {"", "", "शत", "सहस्त्र", "सहस्त्र", "लक्ष", "लक्ष", // this line is same as "", "", "hundred", "thousand", "thousand", "lac", "lac"
                "कोटि", "कोटि", "कोटि", "कोटि", "कोटि", // same as "crore" , "crore" , "crore" and so on
                "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", 
                "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु",
                "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", 
                "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद",
                "पद्म", "पद्म", "पद्म", "पद्म", "पद्म",
                "महापद्म", "महापद्म", "महापद्म", "महापद्म", "महापद्म", 
                "खर्व", "खर्व", "खर्व", "खर्व", "खर्व", 
                "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व",
                "समुद्र", "समुद्र", "समुद्र", "समुद्र", "समुद्र", 
                "ओघ", "ओघ", "ओघ", "ओघ", "ओघ", 
                "महोघ", "महोघ", "महोघ", "महोघ", "महोघ"
                };
        if(n >= arr.length) {
            throw new IllegalArgumentException("Not in range");
        } else {
            return arr[n];
        }
    }
    /**
     * This method takes index(position of digit) as the input and return the number of digits need to be taken in consideration of calculation.
     * <p> Like for index 0 only one digit will be passed to get it's word value, for index 1 two digits(00 to 99) need to be passed,
     * for index 2 inly one need to be passed and so on.
     * @param index position of digit
     * @return next index value
     */
    private static int numOfDigitsToInclude(int index) {
        int [] a = {1, 2, 1, 1, 2, 1, 2, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5};
        return a[index];
    }
    /**
     * This method returns next index value for calculations need to be performed.
     * @param index
     * @return
     */
    private static int nextIndex ( int index) {
        int [] a = {-1, -1, 1, 2, 2, 4, 4, 6, 6, 6, 6, 6, 11, 11, 11, 11, 11,
                16, 16, 16, 16, 16,
                21, 21, 21, 21, 21, 
                26, 26, 26, 26, 26, 
                31, 31, 31, 31, 31, 
                36, 36, 36, 36, 36, 
                41, 41, 41, 41, 41, 
                46, 46, 46, 46, 46, 
                51, 51, 51, 51, 51, 
                56, 56, 56, 56, 56, 
                61, 61, 61, 61, 61
        };
        return a[index];
    }

    /**
     * This method takes number as input in String(as it exceeds the limit for long) form.
     * <p> Convert into array and loop over it to get word value for number.
     * @param num
     * @return
     */
    private static String getWord(String num) {
        if(num.length() == 0) {
            throw new IllegalArgumentException("Not valid");
        }
        if(num.equals("0")){
            return getValue(0);
        }
        String s = "" + num;
        int size = s.length();
        long [] a = new long [size];
        String n = num; int k=0;

        for(int j = num.length() -1 ; j >= 0; j--) {
            a[k++] = Integer.parseInt(""+ num.charAt(j) );
        }
        int i = a.length-1;
        int arg = 0;
        int include = 0;
        StringBuffer buffer = new StringBuffer();
        while(i >= 0) {
            arg = 0;
            include = numOfDigitsToInclude(i);
            if(include == 2) {
                arg = (int) ((int) 10*a[i] + a[i-1]);
            } else if(include == 1) {
                arg = (int) a[i];
            } else {
                for(int l = 1; l<=include; l++) {
                    arg =  (int) (10*arg + a[i+1-l]);
                }
                //recursion used for arg value more than 99
                buffer.append(getWord(""+ arg)).append(" ");
            }
            if(arg == 0) {
                i = nextIndex(i);
                if(i <= 0) {
                    continue;
                }
                continue;
            }
            if(include <= 2) {
                buffer.append(getValue(arg)).append( " ");
            }
            buffer.append(getPlaceValue(i)).append(" ");
            i = nextIndex(i);
        }
        return buffer.toString();
    }
}
于 2019-11-06T22:21:25.983 回答