思路和具体方法都在注释中说明
package ClassicArithmetic;
import java.util.Scanner;
/*
* @author:LOHAS翀
* @time:2019/11/1
*
* 人民币大小写转换(阿拉伯数字转汉字)
* 要求:
* 1)输入小写数字金额,输出大写汉字金额
* 2)最大单位支持到亿,最小单位主持到分(即阿拉伯数字支持小数点后两位),超出范围输出错误提示
* 3)对输入进行校验,不能出现运行错误
* 4)符合人民币金额格式,如:亿/万/元等前不可出现零等
* */
public class UnitTranslate {
//定义数字对应的汉字
public final static String[] num = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
//定义单位
public final static String[] unit = {"", "拾", "佰", "仟"};
public static void main(String[] args) {
while (true) {
try {
System.out.println("请输入数字:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
//对数字进行整数和小数的分隔
String[] numString = input.split("\\.");
//若是整数则将其转换为有小数的数
if (numString.length == 1) {
input += ".0";
numString = input.split("\\.");
}
//将整数进行分隔
String[] zhengshu = numString[0].split("");
//将数字前的0去掉
while (zhengshu[0].equals("0") && zhengshu.length > 1) {
String[] tmp = new String[zhengshu.length - 1];
for (int i = 0; i < zhengshu.length - 1; i++) {
tmp[i] = zhengshu[i + 1];
}
zhengshu = tmp;
}
//对小数进行分隔
String[] xiaoshu = numString[1].split("");
//若格式不对则抛出异常
if (xiaoshu.length > 2 || numString.length != 2 || zhengshu.lenth > 12) {
throw new Exception();
} else if (xiaoshu.length == 1 && xiaoshu[0].equals("0")) {
//小数部分为0的情况
printInfo(zhengshu, xiaoshu);
} else if (xiaoshu.length == 1 && !xiaoshu[0].equals("0")) {
printInfo(zhengshu, xiaoshu);
System.out.print(num[Integer.parseInt(xiaoshu[0])] + "角");
} else if (xiaoshu.length == 2) {
printInfo(zhengshu, xiaoshu);
System.out.print(num[Integer.parseInt(xiaoshu[0])] + "角");
System.out.print(num[Integer.parseInt(xiaoshu[1])] + "分");
}
break;
} catch (NumberFormatException e) {
System.out.println("输入有误");
} catch (Exception e) {
System.out.println("输入有误");
}
}
}
public static void printInfo(String[] zhengshu, String[] xiaoshu) {
int tmp = 0; //该标志位用来记录对unit的操作地址
int zeroNum = 0; //记录从右到左第5到第8位0的个数
//数字为亿级
if (zhengshu.length > 8) {
for (int i = 0; i < zhengshu.length - 8; i++) {
int nums = Integer.parseInt(zhengshu[i]);
//判断相邻是否为0
if (zhengshu[i].equals("0") && zhengshu[i + 1].equals("0") && i + 1 < zhengshu.length - 8) {
continue;
}
//若0在级的最后一位则不输出
if (i == zhengshu.length-8-1 &&zhengshu[zhengshu.length-8-1].equals("0")){
break;
}
//若有相邻的0则输出最后一个0
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
} else {
System.out.print(num[nums] + unit[zhengshu.length - 8 - 1 - i]);
}
}
System.out.print("亿");
for (int i = zhengshu.length - 8; i < zhengshu.length - 4; i++) {
int nums = Integer.parseInt(zhengshu[i]);
if (zhengshu[i].equals("0") && zhengshu[i + 1].equals("0") && i + 1 < zhengshu.length - 4) {
tmp++;
zeroNum++;
continue;
}
if (i == zhengshu.length - 5 && zhengshu[zhengshu.length - 5].equals("0")) {
break;
}
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
tmp++;
} else {
System.out.print(num[nums] + unit[3 - tmp]);
tmp++;
}
}
if (zeroNum != 3) {
System.out.print("万");
}
tmp = 0;
for (int i = zhengshu.length - 4; i < zhengshu.length; i++) {
int nums = Integer.parseInt(zhengshu[i]);
if (i + 1 < zhengshu.length && zhengshu[i].equals("0") && zhengshu[i + 1].equals("0")) {
tmp++;
continue;
}
if (i == zhengshu.length - 1 && zhengshu[zhengshu.length - 1].equals("0")) {
break;
}
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
tmp++;
} else {
System.out.print(num[nums] + unit[3 - tmp]);
tmp++;
}
}
System.out.print("元");
tmp = 0;
} else if (zhengshu.length > 4) { //数字为万级
for (int i = 0; i < zhengshu.length - 4; i++) {
int nums = Integer.parseInt(zhengshu[i]);
if (zhengshu[i].equals("0") && zhengshu[i + 1].equals("0") && i + 1 < zhengshu.length - 4) {
continue;
}
if (i == zhengshu.length - 4 -1 && zhengshu[zhengshu.length-4-1].equals("0")){
break;
}
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
} else {
System.out.print(num[nums] + unit[zhengshu.length - 4 - 1 - i]);
}
}
System.out.print("万");
for (int i = zhengshu.length - 4; i < zhengshu.length; i++) {
int nums = Integer.parseInt(zhengshu[i]);
if (i + 1 < zhengshu.length && zhengshu[i].equals("0") && zhengshu[i + 1].equals("0")) {
tmp++;
continue;
}
if (i == zhengshu.length - 1 && zhengshu[zhengshu.length - 1].equals("0")) {
break;
}
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
tmp++;
} else {
System.out.print(num[nums] + unit[3 - tmp]);
tmp++;
}
}
System.out.print("元");
tmp = 0;
} else if (zhengshu.length <= 4) {
for (int i = 0; i < zhengshu.length; i++) {
int nums = Integer.parseInt(zhengshu[i]);
if ( i + 1 < zhengshu.length && zhengshu[i].equals("0") && zhengshu[i + 1].equals("0")) {
continue;
}
if (i == zhengshu.length-1 && zhengshu[zhengshu.length-1].equals("0")){
break;
}
if (zhengshu[i].equals("0")) {
System.out.print(num[nums]);
} else {
System.out.print(num[nums] + unit[zhengshu.length - 1 - i]);
}
}
System.out.print("元");
}
}
}