JAVA编程 金额转换

发布网友 发布时间:2022-04-23 07:54

我来回答

5个回答

热心网友 时间:2022-06-17 23:49

/**
金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)->(一千零一拾一元 整)输出。
*/
import java.io.*;
import java.lang.String;

public class Money{
public static void main(String[] args)throws Exception{
String str=null;
System.out.println("请输入您的金额¥:");
flag:
while(true){
try{BufferedReader in=
new BufferedReader(new InputStreamReader(System.in));
str=in.readLine();
}catch(IOException e){}

for(int i=0;i<str.length();i++){
if(str.charAt(i)>57||str.charAt(i)<48){
System.out.println("您输入的金额有误!请重新输入");
continue flag;
}
}
break;
}
char[] ch=str.toCharArray();
for(int i=0;i<ch.length;i++){
switch(ch[i]){
case '0':{ ch[i]='零'; break;}
case '1':{ ch[i]='壹'; break;}
case '2':{ ch[i]='贰'; break;}
case '3':{ ch[i]='叁'; break;}
case '4':{ ch[i]='肆'; break;}
case '5':{ ch[i]='伍'; break;}
case '6':{ ch[i]='陆'; break;}
case '7':{ ch[i]='柒'; break;}
case '8':{ ch[i]='捌'; break;}
case '9':{ ch[i]='玖'; break;}
default: ch[i]='f';
}
}

int i=0;
switch(ch.length){

case 0:
case 1: {System.out.println(ch[i]+"元整");}
case 2: {System.out.println(ch[i]+"十"+ch[i+1]+"元整");}
case 3: {System.out.println(ch[i]+"百"+ch[i+1]+"十"+ch[i+2]+"元整");}
case 4: {System.out.println(ch[i]+"千"+ch[i+1]+"百"+ch[i+2]+"十"
+ch[i+3]+"元整"); break;}
case 5: {System.out.println(ch[i]+"万"+ch[i+1]+"千"+ch[i+2]+"百"
+ch[i+3]+"十"+ch[i+4]+"元整"); break;}
case 6: {System.out.println(ch[i]+"十"+ch[i+1]+"万"+ch[i+2]+"千"
+ch[i+3]+"百"+ch[i+4]+"十"+ch[i+5]+"元整"); break;}
case 7: {System.out.println(ch[i]+"百"+ch[i+1]+"十"+ch[i+2]+"万"
+ch[i+3]+"千"+ch[i+4]+"百"+ch[i+5]+"十"+ch[i+6]+"元整"); break;}
case 8: {System.out.println(ch[i]+"千"+ch[i+1]+"百"+ch[i+2]+"十"
+ch[i+3]+"万"+ch[i+4]+"千"+ch[i+5]+"百"+ch[i+6]+"十"+ch[i+7]+"元整"); break;}
case 9: {System.out.println(ch[i]+"亿"+ch[i+1]+"千"+ch[i+2]+"百"
+ch[i+3]+"十"+ch[i+4]+"万"+ch[i+5]+"千"+ch[i+6]+"百"+ch[i+7]+"十"
+ch[i+8]+"元整"); break;}
case 10: {System.out.println(ch[i]+"十"+ch[i+1]+"亿"+ch[i+2]+"千"
+ch[i+3]+"百"+ch[i+4]+"十"+ch[i+5]+"万"+ch[i+6]+"千"+ch[i+7]+"百"+ch[i+8]+"十"
+ch[i+9]+"元整"); break;}

default: System.out.println("错误");

}
}

}

热心网友 时间:2022-06-17 23:49

汗 我们老师也出了这个题

不过我自己已经写了一个程序了..也许不那么完美 但毕竟每个字母都是我自己敲的....

要对得起自己

热心网友 时间:2022-06-17 23:50

前面刚好有作过这个报表,分呢,给我吧!

public class IntToChineseUtil {
static String[] to_9 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
static String[] denom = {"拾","佰","仟","万","亿"};
public static void main(String[] argv){
for(int i=1;i<9999999999l;i++){
String valueStr = toChineseValueStr(i);
System.out.println(valueStr);
}
}
public static String toChineseValueStr(int value){
String valueStr = value+"";
if(valueStr.length()>=9 && valueStr.length()<=12){
int temp1 = value / 100000000;
int temp = value % 100000000;
return toChineseValueStr(temp1)+denom[4]+to5_8(temp,value);
}
if(valueStr.length()>=5 && valueStr.length()<=8)
return to5_8(value,value);
if(valueStr.length()==4)
return to_nnnn(value);
if(valueStr.length()==3)
return to_nnn(value);
if(valueStr.length()==2)
return to_nn(value);
if(valueStr.length()==1)
return to_n(value);
return valueStr;
}
public static String to5_8(int value,int totalValue){
int temp = value/10000;
if(totalValue!=value){
if(value%10000==0)
return to_nnnn(temp)+denom[3];
return to_nnnn(temp)+denom[3]+to_nnnn(value%10000);
}else
if(value%10000==0)
return toChineseValueStr(temp)+denom[3];
return toChineseValueStr(temp)+denom[3]+to_nnnn(value%10000);
}
public static String to_n(int value){
return to_9[value];
}
public static String to_nn(int value){
if(value<10)
return to_9[0]+toChineseValueStr(value);
int t10 = value/10;
int t = value%10;
if(t==0) //刚好是10的倍数
return to_9[t10]+denom[0];
return to_9[t10]+denom[0]+to_9[t];
}
public static String to_nnn(int value){
if(value<100)
return to_9[0]+toChineseValueStr(value);
int t100 = value/100;
int t10 = value%100;
if(t10==0)
return to_9[t100]+denom[1];
return to_9[t100]+denom[1]+to_nn(t10);
}
public static String to_nnnn(int value){
if(value<1000)
return to_9[0]+toChineseValueStr(value);
int t1000 = value/1000;
int t100 = value%1000;
if(t100==0)
return to_9[t1000]+denom[2];
return to_9[t1000]+denom[2]+to_nnn(t100);
}
}

热心网友 时间:2022-06-17 23:50

这种代码网上多的是,有的是JAVAScript写的,你可以搜索一下,一大巴

热心网友 时间:2022-06-17 23:51

我测试了一下,1楼的勉强过的去。分给一楼的吧

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com