常见面试代码题练习(①数字金额的汉字转化)


import java.util.Scanner;

/**
 * 将输入的数字转化为大写金额:
 * 一万以内:1,11,101,1001,1111,1056
 * 一万到一亿:11111,10023,11023,1111000,1100000
 * 一亿:100000000
 * 缺点:int值有上限,没有针对这一块做处理
 *            此处给的是最简单的转化,没有加上小数情形,金额转化的话最小单位为角,分,只需要在基础上做扩充就好
 * @author Oliver
 *
 */
public class TransFromNumToHanZi {
	public static final String ZERO = "零";
	public static final String ONE = "壹";
	public static final String TWO = "贰";
	public static final String THREE = "叁";
	public static final String FOUR = "肆";
	public static final String FIVE = "伍";
	public static final String SIX  = "陆";
	public static final String SEVEN= "柒";
	public static final String EIGHT = "捌";
	public static final String NINE = "玖";
	public static final String SHI = "拾";
	public static final String BAI = "佰";
	public static final String QIAN = "仟";
	public static final String WAN = "萬";
	public static final String YI = "亿";
	public static final String ZHAO = "兆";
	
	public static void main(String args[]){
		//先输入一个数字:
		while(true){
			System.out.print("Please enter a num:");
			Scanner scan = new Scanner(System.in);
			int money = scan.nextInt();
			if(money<=0){
				System.out.print("Bye~");
				break;
			}
			System.out.print("The result is:\n"+transMoney(money)+"\n");
		}
		
	}
	
	public static String transMoney(int money){
		if( money>0){
			String result = "";
			// 当金额大于0,则对金额进行遍历:
			for(int i=0; money>0; i++){
				int tmp = money%10; // 取余数
				money = money/10;//进制
				// 将数值转化为汉字:
				result = transNum(tmp)+(transIndex(i,tmp))+result;
			}
			while(result.contains(ZERO+ZERO)){// 根据规定的规范进行调整,连续的零只读一个
				result = result.replaceAll(ZERO+ZERO, ZERO);
			}
			if(result.contains(ZERO+WAN)){// 零万不读
				result = result.replaceAll(ZERO+WAN, WAN);
			}
			if(result.contains(ZERO+YI)){// 零亿不读零
				result = result.replaceAll(ZERO+YI, YI);
			}
			if(result.contains(ZERO+ZHAO)){// 零兆不读零
				result = result.replaceAll(ZERO+ZHAO, ZHAO);
			}
			if(result.contains(YI+WAN)){// 亿万不读万
				result = result.replaceAll(YI+WAN, YI);
			}
			if(result.contains(ZHAO+YI)){// 兆亿不读亿
				result = result.replaceAll(ZHAO+YI, ZHAO);
			}
			// 最后的零不读:
			if(result.endsWith(ZERO)){
				result = result.substring(0, result.length()-1);
			}
			return result;
		}else{
			return ZERO;
		}
	}
	
	//将数字转为大写汉字形式
	public static String transNum(int num){
		switch(num){
			case 0: return ZERO;
			case 1: return ONE;
			case 2: return TWO;
			case 3: return THREE;
			case 4: return FOUR;
			case 5: return FIVE;
			case 6: return SIX;
			case 7: return SEVEN;
			case 8: return EIGHT;
			case 9: return NINE;
		}
		return ZERO;
	}
	// 将位数转为对应的大小写
	public static String transIndex(int index, int num){
		// 如果num等于0 且index不为万,亿,兆,直接返回空
		// 如果num等于0 且index为万、亿、兆,返回万、亿、兆
		String result = "";
		int index0 = index%4; //索引取余,区分个十百千
		int index1 = index/4;// 索引取商,区分万,亿,兆
		if(num != 0){
			switch(index0){
			case 1: result += SHI;break;// 十位
			case 2: result += BAI;break;// 百位
			case 3: result += QIAN;break;// 千位
			}
		}
		if(index0 == 0){// 仅在万,亿,兆上加标
			switch(index1){
			case 1: result += WAN; break; // 万位
			case 2: result += YI;  break;// 亿位
			case 3: result += ZHAO; break;// 兆位
			}
		}
		return result;
	}
}

你可能感兴趣的:(Java基础)