Java面试题将数字转换为英语方法实例教程。21世纪什么最重要?人才?NO,是面试~
记得我一同学去面试,其中有一面试题是将输入的数字转换为英语单词,回来写下,又在网上参考了些资料,代码如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package name.zrl; public class InputDemo { static String[] to_19 = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; static String[] tens = { "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; static String[] denom = { "" , "thousand" , "million" , "billion" , "trillion" , "quadrillion" , "quintillion" , "sextillion" , "septillion" , "octillion" , "nonillion" , "decillion" , "undecillion" , "duodecillion" , "tredecillion" , "quattuordecillion" , "sexdecillion" , "septendecillion" , "octodecillion" , "novemdecillion" , "vigintillion" }; public static void main(String[] argv) throws Exception { int tstValue = 1234 ; // 一千二百三十四 InputDemo itoe = new InputDemo(); System.out.println(itoe.english_number(tstValue)); } private String convert_nn( int val) throws Exception { if (val < 20 ) return to_19[val]; int flag = val / 10 - 2 ; if (val % 10 != 0 ) return tens[flag] + "-" + to_19[val % 10 ]; else return tens[flag]; } private String convert_nnn( int val) throws Exception { String word = "" ; int rem = val / 100 ; int mod = val % 100 ; if (rem > 0 ) { word = to_19[rem] + " hundred" ; if (mod > 0 ) { word = word + " " ; } } if (mod > 0 ) { word = word + convert_nn(mod); } return word; } public String english_number( int val) throws Exception { if (val < 100 ) { return convert_nn(val); } if (val < 1000 ) { return convert_nnn(val); } for ( int v = 0 ; v < denom.length; v++) { int didx = v - 1 ; int dval = new Double(Math.pow( 1000 , v)).intValue(); if (dval > val) { int mod = new Double(Math.pow( 1000 , didx)).intValue(); int l = val / mod; int r = val - (l * mod); String ret = convert_nnn(l) + " " + denom[didx]; if (r > 0 ) { ret = ret + ", " + english_number(r); } return ret; } } throw new Exception( "Should never get here, bottomed out in english_number" ); } } |
打印数字:1234(一千二百三十四)抓图。
优化后的代码:
Java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package name.zrl; public class ListTest { static String[] to_19 = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; static String[] tens = { "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; static String[] denom = { "" , "thousand " , "million" , "billion" , "trillion" , "quadrillion" , "quintillion" , "sextillion" , "septillion" , "octillion" , "nonillion" , "decillion" , "undecillion" , "duodecillion" , "tredecillion" , "quattuordecillion" , "sexdecillion" , "septendecillion" , "octodecillion" , "novemdecillion" , "vigintillion" }; public static void main(String[] argv) throws Exception { long tstValue = 1234567890987654321L; ListTest itoe = new ListTest(); System.out.println(itoe.english_number(tstValue)); } private String convert_nn( int val) { if (val < 20 ) return to_19[val]; int flag = val / 10 - 2 ; if (val % 10 != 0 ) return tens[flag] + "-" + to_19[val % 10 ]; else return tens[flag]; } private String convert_nnn( int val) { String word = "" ; int rem = val / 100 ; int mod = val % 100 ; if (rem > 0 ) { word = to_19[rem] + " hundred " ; } if (mod > 0 ) { word = word + convert_nn(mod); } return word; } public String english_number( long val) { if (val < 100 ) { System.out.println(( int ) val); return convert_nn(( int ) val); } if (val < 1000 ) { return convert_nnn(( int ) val); } for ( int v = 0 ; v < denom.length; v++) { int didx = v - 1 ; long dval = new Double(Math.pow( 1000 , v)).longValue(); if (dval > val) { long mod = new Double(Math.pow( 1000 , didx)).longValue(); int l = ( int ) (val / mod); long r = ( long ) (val - (l * mod)); String ret = convert_nnn(l) + " " + denom[didx]; if (r > 0 ) { ret = ret + ", " + english_number(r); } return ret; } } return null ; } } |
打印结果:
one quintillion, two hundred thirty-four quadrillion, five hundred sixty-seven trillion, eight hundred ninety billion, nine hundred eighty-seven million, six hundred fifty-four thousand , three hundred twenty-one
以上代码由Java80.cn 疯狂的代码 写的。