将数字转化为汉字表达

来源:互联网 发布:男生轻微sm知乎 编辑:程序博客网 时间:2024/04/28 15:45
/*需求:将输入的数字转化为汉子读出来的形式作者:zsq;date:2013/10/17 17:22*/import java.io.*;import java.util.*;import java.lang.*;public class Get{public static char []c2={'壹','贰','叁','肆','伍','陆','柒','捌','玖'};public static char []c={'亿','万',' '};public static char []c1={'千','百','十',' '};public static void main(String []args)throws Exception{while(true){BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//读取键盘录入。String sb=br.readLine();if(sb.length()>=11){System.out.println("超出了计数范围。。。。。最高10位");return;}Integer str=Integer.parseInt(sb);//十一位越界(Integet只有11位)sb=str.toString();System.out.print(sb);int count=0;//大的循环循环次数(亿,万,个。。)(取值在 3,2,1,)int high=0;//记录最高位个数(取值为0,1,2,3)System.out.print(sb.length());int deep=0;char []a1=sb.toCharArray();if((high=sb.length()%4)==0){count=sb.length()/4;//整除high=4;}else{count=(sb.length()/4)+1;//不整除,循环次数多一次}System.out.println("high:"+high+" \r\n count: "+count+"   ");get(a1,count,high);}}public static void get(char a1[],int count,int high){for(int i=0;i<count;i++){//最外面的大循环,int high1=high%4;int count1=count;if(high1==0)high1=4;for(int j=0;j<high;j++){String s=""+a1[j]+"";int cj2=Integer.parseInt(s);//将输入的字符串转化为整数(去除最高位是0的情况。。例如:0001---->1)if(a1[j]=='0'&&a1[j-1]=='0'&&j>=1){j++;continue;}System.out.print(c2[cj2-1]);//打印的是数字。。汉字System.out.print(c1[4-high1]);//打印循环内的单位high1--;}high=high+4;//读下一个四位if(high>4)high=4;//下面打印的是大循环执行的结尾,,即打印每四位的单位if(count==3)System.out.print(c[i]);else if(count==2)System.out.print(c[i+1]);elseSystem.out.print(c[i+2]);}}          }


这有 有两种情况没有写: 1.是中间出现0的时候;解决思路:判断本位是否为0,如果为0,则打印一个“零”,则判断本位的前一位是否为0,如果为0,则continue;

2.后面全为0的时候,没有思路,望牛人指教

原创粉丝点击