[Java] 纯文本查看 复制代码
import java.util.Scanner;
class Swap {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){ //死循环,
System.out.println("请输入一个数:");
int x = sc.nextInt(); //键盘录入数字
if(x>=0 & x< 1000){ //判断在该范围内,进入
if(x < 10){ //0-9的范围
System.out.println(x+"\t"+getNum(x));
}else if(x < 100){ //10-99的范围
int ge = x % 10; //取个位数
int shi = x / 10 % 10; //取十位数
System.out.println(x+"\t"+getNum(shi)+"十"+getNum(ge));
}else if(x < 1000){ //100-999的范围
int ge = x % 10; //取个位
int shi = x / 10 % 10; //取十位数
int bai = x / 100 % 10; //取百位数
if(shi==0){ //为了人性化的阅读,将十位为零的情况分了出来
System.out.println(x+"\t"+getNum(bai)+"百"+"零"+getNum(ge));
}else{
System.out.println(x+"\t"+getNum(bai)+"百"+getNum(shi)+"十"+getNum(ge));
}
}
}else{
break;
}
}
}
public static char getNum(int index){
char[] arr = {'零','一','二','三','四','五','六','七','八','九'};
return arr[index];
}
}