将数字转化为汉字表达

/*

	需求:将输入的数字转化为汉子读出来的形式
	作者: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]);
			
			else
				System.out.print(c[i+2]);
			}
	}          

}


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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值