我想在其中的等效单词中转换数字。我写下了代码,并为此目的使用了Java字典。我的代码对于三位数的数字效果很好。但是,如果数字超过3位数,它将无法产生输出。同样,如果一个位置是zro且十个位置是从十到十九,则它也不起作用。
我的代码
public class IntegerEnglish {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter the integer");
int input_number=in.nextInt();
int a[]=new int[3];//define array for 3 digit but it should depend upon countdigit. But I cannot specify countdigit here because it is written after this line.
int countdigit=0,i=0;
Dictionary numbers_converter=new Hashtable();//Represent ones position
Dictionary number_place=new Hashtable();//Represent thousand,ten thousand,tem million position
Dictionary number_2nd=new Hashtable();//Represent tens position
Dictionary number_converter1=new Hashtable();//Represent position ten to nineteen
numbers_converter.put(1,"One");
numbers_converter.put(2,"Two");
numbers_converter.put(3,"Three");
numbers_converter.put(4,"Four");
numbers_converter.put(5,"Five");
numbers_converter.put(6,"Six");
numbers_converter.put(7,"Seven");
numbers_converter.put(8,"Eight");
numbers_converter.put(9,"Nine");
number_converter1.put(10,"Ten");
number_converter1.put(11,"Eleven");
number_converter1.put(12,"Twelve");
number_converter1.put(13,"Thirteen");
number_converter1.put(14,"Fourteen ");
number_converter1.put(15,"Fifteen");
number_converter1.put(16,"Sixteen");
number_place.put(3,"Hundred");
number_place.put(4,"Thousand");
number_place.put(7,"Million");
number_place.put(11,"Billion");
number_2nd.put(2,"Twenty");
number_2nd.put(3,"Thirty");
number_2nd.put(4,"Forty");
number_2nd.put(5,"Fifty");
number_2nd.put(6,"Sixty");
number_2nd.put(7,"Seventy");
number_2nd.put(8,"Eighty");
number_2nd.put(9,"Ninty");
while(input_number>0){
a[i] = input_number % 10;
i++;
input_number = input_number / 10;
countdigit = countdigit + 1;//Counter that calculate how many digit are there in this number.
if(countdigit==3||countdigit==4||countdigit==7||countdigit==11) {
System.out.print(numbers_converter.get(a[2]));
System.out.print(number_place.get(countdigit));//Statement for thousand,ten thousand ....position
}
}
System.out.print(number_2nd.get(a[1]));//Like hard code only work when number is 3 digit.
System.out.print(numbers_converter.get(a[0]));//Need some counter which used to change the cell number depend upon number of digit
}
}
输出:输入整数
123 一百二十三
到目前为止,我看到了这里提供的各种示例。没有人使用Java词典。那么,我该如何继续使我的代码对多达十亿个数字有用?