[Java] 纯文本查看 复制代码
package test;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入您要转换的数字");
String str = sc.next();
String redex = "\\d*";
if(str.matches(redex)){
char[] charArray = str.toCharArray();
if(charArray.length==1){
ge(charArray[0]);
System.out.println();
}
else if(charArray.length==2){
shi(charArray[0]);
ge(charArray[1]);
System.out.println();
}
else if(charArray.length==3){
bai(charArray[0]);
shi(charArray[1]);
ge(charArray[2]);
System.out.println();
}
else{
System.out.println("您輸入的整數大於999,請重新輸入!");
}
}
else{
System.out.println("您輸入的不是0-999的整數,請重新輸入!");
}
}
}
public static void ge(char c){
switch(c){
case '1':
System.out.print("一");break;
case '2':
System.out.print("二");break;
case '3':
System.out.print("三");break;
case '4':
System.out.print("四");break;
case '5':
System.out.print("五");break;
case '6':
System.out.print("六");break;
case '7':
System.out.print("七");break;
case '8':
System.out.print("八");break;
case '9':
System.out.print("九");break;
case '0':
System.out.print("零");break;
}
}
public static void shi(char c){
if(c!='0'){
ge(c);
System.out.print("十");
}
}
public static void bai(char c){
if(c!='0'){
ge(c);
System.out.print("百");
}
}
}