package cn.itcast.test01;
import java.util.HashMap;
import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while(true){
System.out.println("请输入一个-1到1000的正整数");
int num=sc.nextInt();
if(num<1000&&num>-1)
numToChina(num);
else
break;
}
}
private static void numToChina(int num) {
HashMap<Integer, String>map = new HashMap<Integer, String>();
map.put(0, "零");
map.put(1, "一");
map.put(2, "二");
map.put(3, "三");
map.put(4, "四");
map.put(5, "五");
map.put(6, "六");
map.put(7, "七");
map.put(8, "八");
map.put(9, "九");
if(num<10)
System.out.println(map.get(num));
else if(num>9&&num<100){
if(num%10==0)
System.out.println(map.get(num/10)+"十");
else
System.out.println(map.get(num/10)+"十"+map.get(num%10));
}else if(num>99&&num<1000){
int b=num/100, s=(num/10)%10, g=num%10;
//十位个位都是0
if(s==0&&g==0)
System.out.println(map.get(b)+"百");
//十位不为0个位是0
else if(s!=0&&g==0)
System.out.println(map.get(b)+"百"+map.get(s)+"十");
//十位是0个位不是0
else if(s==0&&g!=0)
System.out.println(map.get(b)+"百零"+map.get(g));
//个位十位都不0
else
System.out.println(map.get(b)+"百"+map.get(s)+"十"+map.get(g));
}
}
}
|