一道java笔试题,以为很简单,结果半天都没写对!请大虾给出个正确解答
给一个阿拉伯数字
例如输入:123456789
输出:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖
我以为这个功能很简单,但是发现如果数字中间出现多个0的情况,就要有问题
贴一下我的代码,比较差劲
public static String daxie(int sum)
{
String [] n=new String[10];
n[0]= "零 ";
n[1]= "壹 ";
n[2]= "贰 ";
n[3]= "叁 ";
n[4]= "肆 ";
n[5]= "伍 ";
n[6]= "陆 ";
n[7]= "柒 ";
n[8]= "捌 ";
n[9]= "玖 ";
String [] d=new String[10];
d[0]= " ";
d[1]= " ";
d[2]= "拾 ";
d[3]= "佰 ";
d[4]= "仟 ";
d[5]= "萬 ";
d[6]= "拾 ";
d[7]= "佰 ";
d[8]= "仟 ";
d[9]= "亿 ";
//计算数字的位数
int wei=(int)Math.floor
(
Math.log10((double)sum)
)+1;
System.out.println(wei);
String str= " ";
int digit=0;
for (int i=wei;i> 0;i--)
{
digit=sum/
(int)Math.pow(10.0, (double)(i-1));
str+=n[digit].toString()+d[i].toString();
sum=sum%