我正在尝试使用单词打印三位数的代码。 但如果右边的前两位数字介于11和11之间,则无法正常工作。 19(包括)。
任何帮助?
package com;
import java.util.Stack;
public class TestMain {
public static void main(String[] args) {
Integer i=512;
int temp =i;int pos=1;
Stack<String> stack=new Stack<String>();
while(temp>0){
int rem=temp%10;
temp=temp/10;
if(rem!=0){stack.push(getString(rem, pos));}
pos*=10;
}
do{
System.out.print(stack.pop()+" ");
}while(!stack.isEmpty());
}
static String getString(int i,int position){
String str=null;
if(position==10){
i*=position;
}
switch(i){
case 1:
str= "One";break;
case 2:
str= "two";break;
case 3:
str= "three";break;
case 4:
str= "four";break;
case 5:
str= "five";break;
case 6:
str= "six";break;
case 7:
str= "seven";break;
case 8:
str= "eight";break;
case 9:
str= "nine";break;
case 10:
str= "Ten";break;
case 11:
str= "Eleven";break;
case 12:
str= "Twelve";break;
case 13:
str= "thirteen";break;
case 14:
str= "fourteen";break;
case 15:
str= "fifteen";break;
case 16:
str= "sixteen";break;
case 17:
str= "seventeen";break;
case 18:
str= "eighteen";break;
case 19:
str= "Nineteen"; break;
case 20:
str= "twenty";break;
case 30:
str= "Thirty";break;
case 40:
str= "forty";break;
case 50:
str= "Fifty";break;
case 60:
str= "sixty";break;
case 70:
str= "Seventy";break;
case 80:
str= "Eighty"; break;
case 90:
str= "Ninety";break;
case 100:
str= "Hundred";
break;
}
if(position>=100){
str=str+ " "+getString(position, 0);
}
return str;
}
}
答案 0 :(得分:5)
对于三位数字(负数或正数),您只需要将其分成几个部分,并记住小于20的数字是特殊的(在任何一百块中)。作为伪代码(嗯,Python,实际上但足够接近伪代码),我们首先定义查找表:
nums1 = ["","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen","eighteen",
"nineteen"]
nums2 = ["","","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety"]
请注意,10到19之间的数字没有onety
。如前所述,每百个街区中的二十个以下的数字是专门处理的。
然后我们有一个函数,它首先检查输入值并处理负数作为一级递归调用。它还处理零的特殊情况:
def speak (n):
if n < -999 or n > 999:
return "out of range"
if n < 0:
return "negative " + speak (-n)
if n == 0:
return "zero"
下一步是计算出数字中的三位数字:
hundreds = int (n / 100)
tens = int ((n % 100) / 10)
ones = n % 10
数以百计很简单,因为它只有零到九:
if hundreds > 0:
retstr = nums1[hundreds] + " hundred"
if tens != 0 or ones != 0:
retstr = retstr + " and "
else:
retstr = ""
其余的只是将0到19之间的值视为特殊值,否则我们将其视为X
ty Y
(如forty two
或seventy seven
):
if tens != 0 or ones != 0:
if tens < 2:
retstr = retstr + nums1[tens*10+ones]
else:
retstr = retstr + nums2[tens]
if ones != 0:
retstr = retstr + " " + nums1[ones]
return retstr
底部有一个快速而又脏的测试套件:
for i in range (-1000, 1001):
print "%5d %s"%(i, speak (i))
产生
-1000 out of range
-999 negative nine hundred and ninety nine
-998 negative nine hundred and ninety eight
:
-2 negative two
-1 negative one
0 zero
1 one
2 two
:
10 ten
11 eleven
12 twelve
:
998 nine hundred and ninety eight
999 nine hundred and ninety nine
1000 out of range
答案 1 :(得分:4)
以下是Java中的代码:
private static String[] DIGIT_WORDS = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
private static String[] TENS_WORDS = {
"ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety" };
private static String[] TEENS_WORDS = {
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
private static String getHundredWords(int num) {
if (num > 999 || num < 0)
throw new IllegalArgumentException(
"Cannot get hundred word of a number not in the range 0-999");
if (num == 0) return "zero";
String ret = "";
if (num > 99) {
ret += DIGIT_WORDS[num / 100] + " hundred ";
num %= 100;
}
if (num < 20 && num > 9) {
ret += TEENS_WORDS[num % 10];
} else if (num < 10 && num > 0) {
ret += DIGIT_WORDS[num];
} else if (num != 0) {
ret += TENS_WORDS[num / 10 - 1];
if (num % 10 != 0) {
ret += " " + DIGIT_WORDS[num % 10];
}}
return ret;
}
答案 2 :(得分:2)
答案 3 :(得分:2)