代碼一:
import java.util.Scanner;
public class test {
public static void main(String args[]) {
int d,a,b=1,t=0;
String s="";
String [] shu={"零","一","二","三","四","五","六","七","八","九"};
String [] wei={"十","百","千","萬","十","百","千","億","十","百","千","萬"};
Scanner sc=new Scanner(System.in);
d=sc.nextInt();
while(d!=0){
a=d%10;
if(a==0){
if(t!=0&&b!=0)
s=shu[0]+s;
}else if(a>0&&a<10){
s=shu[a]+s;
}
if(d/10!=0&&(d/10%10!=0||t==3))
s=wei[t]+s;
t++;
d=d/10;
b=a;
}
String ss=s.replaceAll("零萬","萬零");
System.out.println(ss);
}
}
代碼二:
import java.io.*;
public class change{
public String NoTranslate(String sNumber){
String[] oneUnit = {"元","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","兆","拾","佰","仟"};
String[] twoUnit = {"分","角"};
String[] sChinese = {"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
int pointPos = sNumber.indexOf("."); //小數點的位置
String sInteger;//記錄整數部分
String sDecimal;//記錄小數部分
String value="";//記錄返回結果
if(pointPos!=-1){
//分解並記錄整數部分,注意substring()的用法
sInteger=sNumber.substring(0,pointPos);
//分解並記錄小數部分
sDecimal=sNumber.substring(pointPos+1,
pointPos+3<sNumber.length()?pointPos+3:sNumber.length());
}else{
//沒有小數部分的情況
sInteger=sNumber;
sDecimal="";
}
//格式化整數部分,並記錄到返回結果
for(int i=0;i<sInteger.length();i++){
int temp = Integer.parseInt(sInteger.substring(i,i+1));
value += sChinese[temp]+oneUnit[sInteger.length()-i-1];
}
//格式化小數部分,並記錄到返回結果
for(int i=0;i<sDecimal.length();i++){
int temp = Integer.parseInt(sDecimal.substring(i,i+1));
value += sChinese[temp]+twoUnit[sDecimal.length()-i-1];
}
//返回結果
return(value);
}
public static void main(String argument[]){
String value="";
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
value=br.readLine();
}catch(IOException e){}
change c=new change();
System.out.println(c.NoTranslate(value));
}
}