将10,000,000以内的数字翻译为英文
题目来源:HJ42 学英语
import java.util.*;
public class Main{
public static void main(String[] args) {
String[] digit = {"one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen"};
String[] digit2 = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
Scanner sc = new Scanner(System.in);
long num = sc.nextLong();
StringBuilder sb = new StringBuilder();
long cur = num/1000000;
if(cur>0){
sb.append(digit[(int)cur-1]);
sb.append(" million ");
}
num=num%1000000;
cur = num/1000;
if(cur>0){
long temp = cur/100;
if(temp>0){
sb.append(digit[(int)temp-1]);
sb.append(" hundred ");
}
cur%=100;
temp = cur/10;
if(temp>1){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit2[(int)temp-2]+" ");
cur%=10;
if(cur>0){
sb.append(digit[(int)cur-1]+" ");
}
}else if(temp>0){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit[(int)cur-1]+" ");
}else {
cur%=10;
if(cur>0){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit[(int)cur-1]+" ");
}
}
sb.append("thousand ");
}
cur = num%1000;
if(cur>0){
long temp = cur/100;
if(temp>0){
sb.append(digit[(int)temp-1]);
sb.append(" hundred ");
}
cur%=100;
temp = cur/10;
if(temp>1){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit2[(int)temp-2]+" ");
cur%=10;
if(cur>0){
sb.append(digit[(int)cur-1]+" ");
}
}else if(temp>0){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit[(int)cur-1]+" ");
}else {
cur%=10;
if(cur>0){
if(sb.toString().endsWith("hundred ")){
sb.append("and ");
}
sb.append(digit[(int)cur-1]+" ");
}
}
}
System.out.println(sb.toString().trim());
}
}