/*
* 利用 %和 / 运算符求得每一位数字并保存到数组,然后通过该数组输出结果
*/
import java.util.Scanner;
public class ToChinese {
private static final char[] CHINESE = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '百', '千' };
private static int num = 0, arrayRealLength = 0;
private static int[] array = new int[3];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while ((num = getNum(scanner)) != -1) {
arrayRealLength = 0;
getArray(num, array);
getChinese(array);
}
}
private static void getArray(int num, int[] array) {
for (int i = 0; i < array.length; i++) {
if (num == 0)
break;
array[i] = num % 10;
num /= 10;
arrayRealLength++;
}
}
private static int getNum(Scanner sc) {
int num = sc.nextInt();
if (num < -1 || num > 1000)
return -1;
return num;
}
private static void getChinese(int[] array) {
if (num == 0) {
System.out.print("" + CHINESE[0]);
return;
}
int lowOrderBit = -1;
if (num % 10 == 0) {
if (num % 100 == 0)
lowOrderBit = 1;
else
lowOrderBit = 0;
}
for (int i = arrayRealLength - 1; i > lowOrderBit; i--) {
System.out.print("" + CHINESE[array[i]]);
if (array[i] != 0 && i > (lowOrderBit + 1)) // 排除10或100的倍数
System.out.print(CHINESE[i + 9]);
else if (num % 100 == 0)
System.out.print(CHINESE[11]);
else if (num % 10 == 0)
System.out.print(CHINESE[10]);
}
System.out.println();
}
}
|