疯狂Java讲义第四章练习2.浮点数转换人民币读法

一篇关于使用Java编程将浮点数转换为人民币读法的练习记录,旨在提升基础和编程技能。
摘要由CSDN通过智能技术生成

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的基础和锻炼编程能力,所做的练习,记录下来留作日后回看。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值