清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | /** * @author sunpander * 把输入的数字转换成人民币大写的形式 */ import java.io.BufferedReader; import java.io.InputStreamReader; public class ChangeNumToRMB { String moneyStrs[] = { "零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" }; public static void main(String[] args) { // TODO Auto-generated method stub boolean isDebug = true ; while (isDebug) { int num = 0 ; String changedStr = "" ; System.out.println( "请输入需要转换的金额:" ); BufferedReader br = new BufferedReader( new InputStreamReader( System.in)); boolean isWrongInput = true ; while (isWrongInput) { try { String str = br.readLine(); num = Integer.parseInt(str); } catch (Exception e) { System.out.println( "输入数字有误,必须是:0" + "到" + Integer.MAX_VALUE + " 的整数" ); // TODO Auto-generated catch block // e.printStackTrace(); isWrongInput = true ; continue ; } isWrongInput = false ; } ChangeNumToRMB chan = new ChangeNumToRMB(); changedStr = chan.getChangedString(num); } } public String getChangedString( int num) { // Money用于存放输入的金额数 int moneyNum = num; String moneyStr = "" ; System.out.println( "输入的钱数是:" ); if (moneyNum < 0 ) { System.out.println( "数字必须大于零" ); } else if (moneyNum == 0 ) { moneyStr = "零元" ; } // 只有四位数,即最大9999.小于10000 else if (moneyNum < 10000 ) { moneyStr = this .getFourNumStr(moneyNum) + "元" ; } // 大于一万,小于一千万 else if (moneyNum <= 10000000 ) // && moneyNum>10000) { int firstFourNum, lastFourNum; firstFourNum = moneyNum / 10000 ; lastFourNum = moneyNum % 10000 ; String firstFourStr = this .getFourNumStr(firstFourNum); String lastFourStr = this .getFourNumStr(lastFourNum); if (!lastFourStr.contains( "仟" )) lastFourStr = "零" + lastFourStr; moneyStr = firstFourStr + "万" + lastFourStr + "元" ; } // 大于一千万.即上亿但小于最大整数 else if (moneyNum < Integer.MAX_VALUE) // && moneyNum>10000000) { int firstFourNum, secondFourNum, lastFourNum; firstFourNum = moneyNum / 100000000 ; secondFourNum = (moneyNum / 10000 ) % 1000 ; lastFourNum = moneyNum % 10000 ; String firstFourStr = this .getFourNumStr(firstFourNum); String secondFourStr = this .getFourNumStr(secondFourNum); String lastFourStr = this .getFourNumStr(lastFourNum); if (!lastFourStr.contains( "仟" )) lastFourStr = "零" + lastFourStr; if (!secondFourStr.contains( "仟" )) secondFourStr = "零" + secondFourStr; moneyStr = firstFourStr + "亿" + secondFourStr + "万" + lastFourStr + "元" ; } else System.out.println( "someting is wrong" ); System.out.println(moneyStr); return moneyStr; } public String getFourNumStr( int fourNum) { int qian, bai, shi, ge; ge = fourNum % 10 ; shi = (fourNum % 100 ) / 10 ; bai = (fourNum % 1000 ) / 100 ; qian = fourNum / 1000 ; String strTemp = "" ; if (qian != 0 ) { strTemp = strTemp.concat(moneyStrs[qian] + "仟" ); strTemp = strTemp.concat(moneyStrs[bai] + "佰" ); strTemp = strTemp.concat(moneyStrs[shi] + "什" ); strTemp = strTemp.concat(moneyStrs[ge]); strTemp = strTemp.replace( "零佰" , "零" ); strTemp = strTemp.replace( "零什" , "零" ); strTemp = strTemp.replace( "零零" , "零" ); strTemp = strTemp.replace( "零零" , "零" ); if (strTemp.endsWith( "零" )) strTemp = strTemp.substring( 0 , strTemp.length() - 1 ); } else if (bai != 0 ) { strTemp = strTemp.concat(moneyStrs[bai] + "佰" ); strTemp = strTemp.concat(moneyStrs[shi] + "什" ); strTemp = strTemp.concat(moneyStrs[ge]); strTemp = strTemp.replace( "零什" , "零" ); strTemp = strTemp.replace( "零零" , "零" ); if (strTemp.endsWith( "零" )) strTemp = strTemp.substring( 0 , strTemp.length() - 1 ); } else if (shi != 0 ) { strTemp = strTemp.concat(moneyStrs[shi] + "什" ); strTemp = strTemp.concat(moneyStrs[ge]); if (strTemp.endsWith( "零" )) strTemp = strTemp.substring( 0 , strTemp.length() - 1 ); } else strTemp.concat(moneyStrs[ge]); return strTemp; } } |