package org.crazy.ch4;
import java.util.Scanner;
public class TurnRmbReading {
private static char[] hanArr = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒',
'捌', '玖' };
private static char[] zhengUnitArr = { '十', '百', '千', '万', '亿' };
private static char[] monUnitArr = { '角', '分', '元' };
private static boolean toZore = false;
public static StringBuffer toHanSiweiStr(String siwei) {
int strLen = siwei.length();
StringBuffer result = new StringBuffer();
for (int i = 0; i < strLen; i++) {
int num = siwei.charAt(i) - 48;
if (i != strLen - 1) { // 当前出的的是否为个位
if (num == 0 && toZore == false) {
toZore = true;
continue;
} else if (num == 0 && toZore == true) {
continue;
} else if (num != 0 && toZore == true) {
toZore = false;
result.append(hanArr[0]).append(hanArr[num])
.append(zhengUnitArr[strLen - 2 - i]);
} else
result.append(hanArr[num]).append(
zhengUnitArr[strLen - 2 - i]);
} else if (i == strLen - 1) {
if (num == 0)
break;
else if (toZore && num != 0) {
toZore = false;
result.append(hanArr[0]).append(hanArr[num]);
} else
result.append(hanArr[num]);
}
}
return result;
}
public static String toHanZhengStr(String zhengshu) {
int strLen = zhengshu.length();
StringBuffer toHan = new StringBuffer();
StringBuffer tmp;
boolean startZore = false;
if (strLen > 12) {
return "浮点数超过长度,无法转换!";
}
if ("0".equals(zhengshu.charAt(0)) || (zhengshu.charAt(0) - 48) == 0)
startZore = true;
if (strLen >= 9) { // 亿以上
tmp = toHanSiweiStr(zhengshu.substring(0, strLen - 8));
if (tmp.length() >= 1)
toHan.append(tmp).append(zhengUnitArr[4]);
}
if (strLen >= 5) { // 万以上
if (strLen >= 9)
tmp = toHanSiweiStr(zhengshu.substring(strLen - 8, strLen - 4));
else
tmp = toHanSiweiStr(zhengshu.substring(0, strLen - 4));
if (tmp.length() >= 1)
toHan.append(tmp).append(zhengUnitArr[3]);
}
if (strLen >= 1) {
if (strLen >= 5)
toHan.append(toHanSiweiStr(zhengshu.substring(strLen - 4,
strLen)));
else
toHan.append(toHanSiweiStr(zhengshu.substring(0, strLen)));
}
if (toHan.length() <= 0)
return toHan.toString();
else {
toHan.append(monUnitArr[2]);
if (startZore == true)
return toHan.substring(1, toHan.length());
else
return toHan.toString();
}
}
public static String toHanXiaoStr(String xiaoshu) {
int strLen = xiaoshu.length();
StringBuffer toHan = new StringBuffer();
if (strLen == 0) {
return null;
} else if (strLen == 1) {
int num = xiaoshu.charAt(0) - 48;
if (num == 0)
return null;
else
toHan.append(hanArr[num]).append(monUnitArr[0]);
}
for (int i = 0; i < 2; i++) {
int num = xiaoshu.charAt(i) - 48;
switch (i) {
case 0:
if (num == 0) {
toZore = true;
continue;
} else if (num != 0 && toZore == true) {
toZore = false;
toHan.append(hanArr[0]).append(hanArr[num])
.append(monUnitArr[i]);
continue;
} else if (num != 0 && toZore == false) {
toHan.append(hanArr[num]).append(monUnitArr[i]);
continue;
}
case 1:
if (num == 0) {
toZore = false;
continue;
} else if (num != 0 && toZore == true) {
toHan.append(hanArr[0]).append(hanArr[num])
.append(monUnitArr[i]);
continue;
}
else if (num != 0 && toZore == false) {
toHan.append(hanArr[num]).append(monUnitArr[i]);
continue;
}
default:
continue;
}
}
return toHan.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String num = scanner.next();
String[] numArr = num.split("\\."); // 需要加转义符的有:"\\.","\\|"
StringBuffer result = new StringBuffer();
String tmp;
for (int i = 0; i < numArr.length; i++) {
switch (i) {
case 0:
tmp = toHanZhengStr(numArr[i]);
if (tmp != null && !"".equals(tmp))
result.append(tmp);
continue;
case 1:
tmp = toHanXiaoStr(numArr[i]);
if (tmp != null && !"".equals(tmp))
result.append(tmp);
continue;
default:
continue;
}
}
System.out.println(result.toString());
}
}
加入为了巩固Java的基础和锻炼编程能力,所做的练习,记录下来留作日后回看。