求各给大神帮忙啊

鬼火儿 2012-10-26 08:30:57
import java.util.*;
public class Translate{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
solv(num);
}
private static void solv(String num){
char[] trans = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
char[] dan ={'拾','佰','仟'};
char[] dan1 = {'万','亿'};
char ch;
int n = 0,l = 0;
int j = 0,temp=0;
int t = 1,c=0, d = 0,f = 0,m=0;
boolean bl = true;
for(int i = 0;i < num.length();i++){
char ch1 = num.charAt(i);
if((ch1 >= '0'&& ch1 <= '9'||ch1 == '.')!= bl){
n++;
break;
}
if(ch1 == '.'){
l++;
d = i;}
if(ch1 >= '0'&&ch1 <= '9'){
c++;
m++;
}

}
if(l > 0)
c = d;
System.out.println(c);
System.out.println(d);
for( j = 0;j < num.length();j++){
if(l > 1||n > 0){
System.out.print("输入错误!!!");
temp++;
break;
}
ch = num.charAt(j);
if(ch >= '0'&& ch <= '9'){

if(ch%48==0){
c--;
if(c == 0&&d!=1)
System.out.print("元");
continue;
}
System.out.print(trans[ch%48]);
if(c==9){
System.out.print(dan1[c / 5]);
c--;
continue;
}

if(c == 5){
System.out.print(dan1[c % 5]);
c--;
continue;
}
if(c-2>=0)
System.out.print(dan[(c-2) % 4]);
}
if(ch != '.')
c--;
else
break;
if(c == 0)
System.out.print("元");
}
if(m-d>0&&num.charAt(d)=='.'&&temp==0){
System.out.print(trans[(num.charAt(d+1)%48)]+"角");
if(m-d>1)
System.out.print(trans[(num.charAt(d+2)%48)]+"分");}

}
}

怎么解决各种“零”情况的出现
...全文
67 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
霸王龙的日常 2012-10-26
  • 打赏
  • 举报
回复
这个问题你可以用这种方法来解决:
讲各种条件作为数组元素来处理
然后用for循环对数组中的条件进行遍历
for循环里面用switch来解决

就是这种想法,你做修改后就可以了,尤其是这种问题,此类方法很实用

int b[n]={(条件一,条件二,…,条件n};
for(j=0;j<n;j++)
{
switch(b[j])
{
case 1:
System.out.println("壹");
break;
case 2:
System.out.println("贰");
break;
case 3:
方法同上 输出"叄";后面的case情况类似
break;
case 4:"肆";
break;
case 5:"伍";
break;
case 6:"陆";
break;
case 7:"柒";
break;
case 8:"捌";
break;
case 9:"玖";
break;
default:"零";
break;

}

if(j==条件中的某个值)
{
System.out.println(""圆整"");根据情况进行相应的格式处理
}

else if(j==比如2)
cout<<"拾";
else if(j==比如1)
cout<<"佰";
else cout<<"仟";
}
}

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧