当前位置: 代码迷 >> Java相关 >> 下面的程序,有比较简单而且还考虑全面的方法!??
  详细解决方案

下面的程序,有比较简单而且还考虑全面的方法!??

热度:133   发布时间:2007-07-15 09:52:55.0
下面的程序,有比较简单而且还考虑全面的方法!??

下面的程序,有比较简单而且还考虑全面的方法!??

金额转换:输入 1562 则输出 一千五百六十二元整
输入 1562.456 则输出 一千五百六十二元四角六分

! !


我有个想法,先判断输入的字符中有没有“.”,没有的话补上,并补齐后面3位,有“.”不足3位的也补齐3位,然后把“.”前的放一个字符串数组中,后面的放一个字符串数组中,分别转义成汉语数字,前面的分为每4位的处理,后面的 角 分 处理,这样该怎么样实现啊???


import java.io.*;
class A
{
private String number[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private String unit[]={"","拾","佰","仟"};
private String small[]={"角","分"};
A()
{

}
private String add(String money)
{
return money;
}
private boolean IsInNumber(String strNumber)
{
boolean inNumber=false;
for (int i=0;i<9;i++)
{
if (strNumber.compareTo (number[i])==0) inNumber=true;
}
return inNumber;
}
private String SplitChineseNumber(int intUnit,String strInt)
{
try
{
int l=strInt.length ();
int j,k,zeorCountTemp=0;
String strUnit="",strNumber="",strAll="";
boolean temp=false;
if(Integer.parseInt(strInt)==0)
return "";
for (k=0;k<l;k++)
{
String strTemp=strInt.substring(k,k+1);
int intTemp=Integer.parseInt(strTemp);
if (intTemp!=0) temp=true;
}
if (temp==false)
{
if (intUnit==5)return "";
}
int a=0;
for (k=0;k<l;k++)
{
String strTemp=strInt.substring(k,k+1);
int intTemp=Integer.parseInt(strTemp);
strNumber= number[intTemp];
j=l-1-k;
strUnit=unit[j];
if (intTemp==0)
{
if (zeorCountTemp==0)
{
strUnit=strUnit.replace('拾','零');
strUnit=strUnit.replace('佰','零');
strUnit=strUnit.replace('仟','零');
}
else
{
strUnit=strUnit.replaceAll("拾","");
strUnit=strUnit.replaceAll("佰","");
strUnit=strUnit.replaceAll("仟","");
}
zeorCountTemp++;
}
a=k;
strAll+=strNumber+strUnit;
}
return strAll;
}
catch(Exception e)
{
System.out.println("超出范围");
return "";
}
}
private String leftChinese(String left)
{
int l=left.length();
String strAll="";
if(l>16)
return "";
if (l>12)
{
strAll+=this.SplitChineseNumber(13,left.substring(0,l-12))+"兆";
strAll+=this.SplitChineseNumber(9,left.substring(l-12,l-8))+"亿";
strAll+=this.SplitChineseNumber(5,left.substring(l-8,l-4))+"万";
strAll+=this.SplitChineseNumber(1,left.substring(l-4,l))+"元";
}
else if(l>8)
{
strAll+=this.SplitChineseNumber(9,left.substring(0,l-8))+"亿";
strAll+=this.SplitChineseNumber(5,left.substring(l-8,l-4))+"万";
strAll+=this.SplitChineseNumber(1,left.substring(l-4,l))+"元";
}
else if (l>4)
{
strAll+=this.SplitChineseNumber(5,left.substring(0,l-4))+"万";
strAll+=this.SplitChineseNumber(1,left.substring(l-4,l))+"元";
}
else if (l>0)
{
strAll+=this.SplitChineseNumber(1,left)+"元";
}
int checkL=strAll.length();
char strTemp2;
for (int k=1;k<checkL;k++)
{
strTemp2=strAll.charAt(k);
if (strTemp2=='零')
{
String strBeforeTemp=strAll.substring(k-1,k);
String strAfterTemp=strAll.substring(k+1,k+2);
if (!this.IsInNumber(strBeforeTemp)&&!this.IsInNumber (strAfterTemp))
{
strBeforeTemp=strAll.substring(0,k);
strAfterTemp=strAll.substring(k+1,checkL);
strAll= strBeforeTemp+strAfterTemp;
break;
}
}
}
return strAll;

}
private String rightChinese(String right)
{
String strAll="", strNumber="",strUnit="";
int intSmall=Integer.parseInt(right);
if (intSmall>=10)
{
for (int i=0;i<2;i++)
{
String strTemp=right.substring(i,i+1);
if (Integer.parseInt(strTemp)!=0)
{
strNumber=number[Integer.parseInt(strTemp)];
strUnit=small[i];
strAll+=strNumber+strUnit;
}
}
}
else
{
if (intSmall!=0)
{
strNumber=number[intSmall];
strUnit=small[0];
strAll+=strNumber+strUnit;
}
else
strAll="整";
}
return strAll;
}
public String getchineseMoney(String money)
{
money=this.add(money);
String strAll,strChineseLeft,strChineseRight;
String left,right;
int index=money.indexOf(".");
left=money.substring(0,index);
//System.out.println(left);
right=money.substring(index+1);
//System.out.println(right);
strChineseLeft=leftChinese(left);
strChineseRight=rightChinese(right);
strAll=strChineseLeft+strChineseRight;
if(strChineseLeft=="")
strAll="超出范围";
return strAll;
}
}
public class chineseMoney
{
public static void main(String args[]) throws IOException
{
try
{
A cm=new A();
String strMoney="",strchineseMoney;
System.out.println("输入货币:");
BufferedReader cin = new BufferedReader(new InputStreamReader (System.in));
strMoney = cin.readLine();
if(strMoney.indexOf(".")==-1)
strMoney=strMoney+"."+"0";
String left=strMoney.substring(0,strMoney.indexOf("."));
String right=strMoney.substring(strMoney.indexOf(".")+1);
long leftlong=Long.parseLong(left);
long rightlong=Long.parseLong(right);
if(left.length()>16)
{
System.out.println("超出范围");
return;

}
strchineseMoney=cm.getchineseMoney(strMoney);
if(strchineseMoney.indexOf("兆")==0)
strchineseMoney=
strchineseMoney.substring(1);
if(strchineseMoney.indexOf("亿")==0)
strchineseMoney=
strchineseMoney.substring(1);
if(strchineseMoney.indexOf("万")==0)
strchineseMoney=
strchineseMoney.substring(1);
if(strchineseMoney.indexOf("元")==0)
strchineseMoney=
strchineseMoney.substring(1);
System.out.println("您输入的金额是:\n"+strchineseMoney );
}
catch(Exception e)
{
System.out.println("你输入的格式不对");
}
}
}

另一种方法 还有一个参考的:


import java.io.*;
public class chineseMoney
{
private String number[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private String unit[]={"","拾","佰","仟"};
private String small[]={"角","分"};

private boolean IsInNumber(String strNumber)
{
boolean inNumber=false;
for (int i=0;i<9;i++)
{
if (strNumber.compareTo (number[i])==0) inNumber=true;
}
return inNumber;
}


private String SplitChineseNumber(int intUnit,String strInt)
{
int l=strInt.length ();
int j,k,zeorCountTemp=0;
String strUnit="",strNumber="",strAll="";
boolean temp=false;
for (k=0;k<l;k++)

{
String strTemp=strInt.substring(k,k+1);
int intTemp=Integer.parseInt(strTemp);

if (intTemp!=0) temp=true;
}
if (temp==false)
{
if (intUnit==5)return "";
}


int a=0;

for (k=0;k<l;k++)
{
String strTemp=strInt.substring(k,k+1);
int intTemp=Integer.parseInt(strTemp);
strNumber= number[intTemp];

j=l-1-k;

strUnit=unit[j];

if (intTemp==0)
{

if (zeorCountTemp==0)
{

strUnit=strUnit.replace('拾','零');
strUnit=strUnit.replace('佰','零');
strUnit=strUnit.replace('仟','零');
}
else
{

strUnit=strUnit.replaceAll("拾","");
strUnit=strUnit.replaceAll("佰","");
strUnit=strUnit.replaceAll("仟","");
}
zeorCountTemp++;
}
a=k;
strAll+=strNumber+strUnit;
}

return strAll;
}

private String onlyInt(int intInt)
{
String strInt;
strInt=String.valueOf(intInt);
int l=strInt.length();

String strAll="";
if (l>12)


{
strAll+=this.SplitChineseNumber(13,strInt.substring(0,l-12))+"兆";
strAll+=this.SplitChineseNumber(9,strInt.substring(l-12,l-8))+"亿";
strAll+=this.SplitChineseNumber(5,strInt.substring(l-8,l-4))+"万";
strAll+=this.SplitChineseNumber(1,strInt.substring(l-4,l))+"元";
}
else if(l>8)
{
strAll+=this.SplitChineseNumber(9,strInt.substring(0,l-8))+"亿";
strAll+=this.SplitChineseNumber(5,strInt.substring(l-8,l-4))+"万";
strAll+=this.SplitChineseNumber(1,strInt.substring(l-4,l))+"元";
}
else if (l>4)
{
strAll+=this.SplitChineseNumber(5,strInt.substring(0,l-4))+"万";
strAll+=this.SplitChineseNumber(1,strInt.substring(l-4,l))+"元";

}
else if (l>0)
{
strAll+=this.SplitChineseNumber(1,strInt)+"元";
}
int checkL=strAll.length();

char strTemp2;
for (int k=1;k<checkL;k++)
{
strTemp2=strAll.charAt(k);
if (strTemp2=='零')
{

String strBeforeTemp=strAll.substring(k-1,k);
String strAfterTemp=strAll.substring(k+1,k+2);
if (!this.IsInNumber(strBeforeTemp)&&!this.IsInNumber (strAfterTemp))
{
strBeforeTemp=strAll.substring(0,k);
strAfterTemp=strAll.substring(k+1,checkL);
strAll= strBeforeTemp+strAfterTemp;
break;
}

}
}

return strAll;

}

private String onlySmall(int intSmall)
{
String strNumber,strUnit,strAll;
strNumber="";strUnit="";strAll="";
String strSmall,strTemp;
strSmall=String.valueOf(intSmall);
int i;
if (intSmall>=10)
{
for (i=0;i<strSmall.length();i++)
{
strTemp=String.valueOf(intSmall).substring(i,i+1);
if (Integer.parseInt(strTemp)!=0)
{
strNumber=number[Integer.parseInt(strTemp)];
strUnit=small[i];
strAll+=strNumber+strUnit;
}
}
}
else
{
if (intSmall!=0)
{
strNumber=number[intSmall];
strUnit=small[1];
strAll+=strNumber+strUnit;
}
}

return strAll;
}

public String getChineseMoney(double number)
{


number=(number*100+0.5)/100;

String strAll,strChineseInt,strChineseSmall,strZheng;;
int intInt,intSmall;
strChineseInt="";strChineseSmall="";strZheng="";


intInt=(int)( number*100/100);
if (intInt!=0)
{
strChineseInt=onlyInt(intInt);
}

double temp=(number-intInt)*100*100/100;

intSmall=(int)(temp*100+0.5)/100;
if (intSmall!=0)
{
strChineseSmall=onlySmall(intSmall);
}
else
{
strZheng="整";
}
strAll=strChineseInt+strChineseSmall+strZheng;
return strAll;
}
public static void main(String args[]) throws IOException
{
chineseMoney cm=new chineseMoney();
double money;
String strMoney,strChineseMoney;
strMoney="";

System.out.println("输入货币(四舍五入):");
BufferedReader cin = new BufferedReader(new InputStreamReader (System.in));
strMoney = cin.readLine();
money=Double.parseDouble(strMoney);

strChineseMoney=cm.getChineseMoney(money);
System.out.println("您输入的金额是:\n"+strChineseMoney );
}

}


----------------解决方案--------------------------------------------------------
呵呵,怎么又是这题?
看看,给点意见嘛
[URL=http://bbs.bc-cn.net/viewthread.php?tid=154788]请单击此处[/URL]

[此贴子已经被作者于2007-7-16 22:01:19编辑过]


----------------解决方案--------------------------------------------------------
如果能写点汉字,我看起来就不会那么痛苦了
----------------解决方案--------------------------------------------------------
先用正则判断是整数还是浮点数 然后 用取余操作获得 千 百 十 ...的数据 都调用转换汉字方法(自己编写 case 0-9就可以了 取数的时候自然获得 ) 字符串合并 完成读操作
----------------解决方案--------------------------------------------------------
都堆代码了!
----------------解决方案--------------------------------------------------------
拷,乱鸡八说什么,!!!!楼上的!&gt;哈哈
----------------解决方案--------------------------------------------------------