台哥算法练习 - 12345变为一万两千三百四十五

这是十年前,刚刚工作时,一次在群里,看到这问题,随手半小时,写下这代码:


package suanfa;
/**
 * 把12345变为一万两千三百四十五
 * 
 * @author 台哥编程课堂
 * https://blog.csdn.net/chaohi
 */
public class RenMinBi {

	String[] str1 = {"","十","百","千","万","十","百","千","亿","十","百","千"};
	String[] str2 = {"","一","二","三","四","五","六","七","八","九"};
	
	public String get(String str){
		String abc = "";
		for (int i = 0; i < str.length(); i++) {
			int number = Integer.parseInt(str.substring(i,i+1));
			if(number!=0){
				abc = abc + str2[number] + str1[str.length()-i-1];
			}else{
				if(str.length()-i-1==1){
					abc = abc + "零";
				}
			}
			
		}
		return abc;
	}
	
	
	public static void main(String[] args){
		String m = "123456789";
		System.out.println(new RenMinBi().get(m));
		
		//执行后的打印结果:一亿二千三百四十五万六千七百八十九
	}

}

 

posted on 2009-02-24 15:16  台哥编程课堂  阅读(259)  评论(0编辑  收藏  举报

导航