import java.util.Arrays; import java.util.Scanner; public class Num2Rmb { private String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private String[] unitArr = {"十", "百", "千", "万"}; private String toHanStr(String numStr) { String result = ""; boolean flag = false; int numLen = numStr.length(); for(int i = 0; i < numLen; i++) { int num = numStr.charAt(i) - 48; if(i < numLen -1&&num != 0){ result += hanArr[num] + unitArr[numLen - 2 - i]; if(flag == false) flag = true; } else{ while(num == 0 && i < numLen - 1){ i++; num = numStr.charAt(i) - 48; } if(flag == false && i == numLen - 1) result += hanArr[num]; else{ if(num != 0 && i < numLen - 1){ int tempnum = numStr.charAt(i - 1) - 48; if(tempnum == 0 && flag) result += hanArr[0]; result += hanArr[num] + unitArr[numLen - 2 - i]; if(flag == false) flag = true; } if(num != 0 && i == numLen -1){ int tempnum = numStr.charAt(i - 1) - 48; if(tempnum == 0) result += hanArr[0]; result += hanArr[num]; if(flag == false) flag = true; } } } } return result; } public static void main(String[] args) { Num2Rmb nr = new Num2Rmb(); Scanner sc = new Scanner(System.in); while(true){ System.out.print("输入任意一个整数:"); String str = sc.nextLine(); System.out.println(nr.toHanStr(str)); } } }
原文:http://www.cnblogs.com/Code-Farmer/p/4057973.html