日期:2014-05-20  浏览次数:20608 次

求各给大神帮忙啊
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)]+"分");}

}
}

怎么解决各种“零”情况的出现
------解决方案--------------------
这个问题你可以用这种方法来解决:
    讲各种条件作为数组元素来处理
    然后用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;