2-34 中文大写金额。声明RMB人民币类如下,实现其中成员方法。public class rMB // 人民币类 {// 返回金额x的中文大写形式字符串,如x=123 45,转化为“壹佰贰拾

发布于:2023-01-04 ⋅ 阅读:(405) ⋅ 点赞:(0)

1.实验题目:

2-34 中文大写金额。

声明RMB人民币类如下,实现其中成员方法。

public class rMB // 人民币类

{

// 返回金额x的中文大写形式字符串,如x=123 45,转化为“壹佰贰拾叁元肆角伍分”

public static string tostring(double x)}

考虑以下多种数据情况实现算法。

①整数金额省略小数部分,添加“整” 字。例如,123 表示为“壹佰贰拾叁元整

② 若金额中含有连续的0,则只写一个 “零”。例如,10005 表示为“壹万零伍元

10 的省略表示形式。例如,110 表示为“壹佰壹拾元整”,而 10 则表示为“拾元

2.实验目的:

实现人民币大小写的转换(阿拉伯数字转汉字)

  1. 实验内容:

1.实验思路

将输入的阿拉伯数字金额转化为中文大写的形式。

通过声明RMB类,编写转化的处理机制。

针对题目中所给情况②设计了check()方法对零进行处理。

针对题目中所给情况①设计了toString()方法判断是否为整数,并判断所给数字有多少位,最后输出转换后的结果。

  1. 关键代码截图

(1)对零的处理

(2)判断是否为小数并获得小数点位置

 

(3)判断是否为整数,并对整数情况进行处理

 

 

(4)对小数情况进行处理

 

3.运行结果:

1对整数情况进行验证:

(2)对小数的情况进行验证:

 

 全部代码:


import java.text.DecimalFormat;
import java.util.Scanner;

public class RMB {
    static char[] IntUnit = {'元', '拾', '佰', '千', '万', '亿'};
    static char[] DecUnit = {'角', '分'};
    static char[] Figure = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};

    public static String check(char[] money) //对于零的处理应该放在最后一次遇到
    {
        String ans = new String();
        int wet = money.length - 1;

        if (wet == 0 && money[0] == '0') //处理金额为小数的情况
        {
            ans += Figure[0];
        } else {
            int sign = 0;//标记是否遇到0
            for (int i = 0; i < money.length; i++) {
                if (sign == 0 && money[i] == '0') //第一次遇到了零
                {
                    sign = 1;
                } else if (sign == 1 && money[i] == '0') //在一连串的零后又遇到了零
                {
                    if (i + 1 < money.length && money[i + 1] == '0') continue; //0的后面还是零且不在最后一位
                    else if (i + 1 < money.length && money[i + 1] != '0') ans += Figure[0]; //0的后面不是零且不在最后一位
                    else if (i == money.length - 1) continue; //到结尾还是零,无需处理
                } else if (sign == 1 && money[i] != '0') //在一连串的零后又遇到了非零数字
                {
                    sign = 0;
                    ans += Figure[Integer.valueOf(money[i]) - 48]; //千万注意Integer.valueOf(money[i])转换的是字符的ascll码值
                    if (i != wet) ans += IntUnit[wet - i]; //不是最后一位才会加上单位
                } else if (sign == 0 && money[i] != '0') //之前一直没有遇到零
                {
                    ans += Figure[Integer.valueOf(money[i]) - 48];
                    if (i != wet) ans += IntUnit[wet - i];
                }
            }
        }
        return ans;
    }

    public static String toString(double x) {
        //将要转化的金额数字格式化
        DecimalFormat df = new DecimalFormat("###0.00");
        //将money[]转化为字符串
        char[] money = df.format(x).toCharArray();
        //定义转化后字符串的整数部分和小数部分
        String IntPart = new String(), DecPart = new String();
        //获得小数点下标位置
        int index = 0;
        for (int i = 0; i < money.length; i++) {
            if (money[i] == '.') {
                index = i;
                break;
            }
        }
        //获取整数金额
        char[] Intpart_ = new char[index];
        System.arraycopy(money, 0, Intpart_, 0, index);
        int elength = 0, wlength = 0; //当存在亿位和万位时,记录其长度
        if (Intpart_.length > 8) //亿
        {
            char[] temp = new char[Intpart_.length - 8];
            elength = Intpart_.length - 8; //存在时记录亿位的长度
            for (int i = 0; i < Intpart_.length - 8; i++)
                temp[i] = money[i];
            IntPart += check(temp);
            IntPart += IntUnit[5];
        }
        if (Intpart_.length > 4) //万
        {
            if (Intpart_.length > 8) //存在亿
            {
                char[] temp = new char[4];
                wlength = 4; //存在时记录万位的长度
                for (int i = 0; i < 4; i++) temp[i] = money[elength + i];
                IntPart += check(temp);
                IntPart += IntUnit[4];
            } else {
                char[] temp = new char[Intpart_.length - 4];
                wlength = Intpart_.length - 4; //存在时记录万位的长度
                for (int i = 0; i < Intpart_.length - 4; i++) temp[i] = money[i];
                IntPart += check(temp);
                IntPart += IntUnit[4];
            }
        }

        // 千
        if (Intpart_.length > 4) //存在万
        {
            char[] temp = new char[4];
            for (int i = 0; i < 4; i++) temp[i] = money[elength + wlength + i];
            IntPart += check(temp);
            IntPart += IntUnit[0];
        } else {
            char[] temp = new char[Intpart_.length];
            for (int i = 0; i < Intpart_.length; i++) temp[i] = money[i];
            IntPart += check(temp);
            IntPart += IntUnit[0];
        }

        char[] XiaoShu = new char[money.length - index - 1];
        System.arraycopy(money, index + 1, XiaoShu, 0, money.length - index - 1); //获取小数金额
        //System.out.println(XiaoShu);

        int isDigit = ((XiaoShu.length == 2) && (XiaoShu[0] == '0') && (XiaoShu[1] == '0')) ? 1 : 0; //金额是否是整数
        if (isDigit == 1) //如果是整数
        {
            IntPart += '整';
            return IntPart;
        }
        //处理小数
        else {
            if (XiaoShu.length == 2 && XiaoShu[1] == '0') //只有一位
            {
                DecPart += Figure[Integer.valueOf(XiaoShu[0]) - 48];
                DecPart += DecUnit[0];
            } else if (XiaoShu.length == 2 && XiaoShu[0] == '0') //两位,第一位为0
            {
                DecPart += Figure[Integer.valueOf(XiaoShu[1]) - 48];
                DecPart += Figure[1];
            } else //两位,都有数字
            {
                DecPart += Figure[Integer.valueOf(XiaoShu[0]) - 48];
                DecPart += DecUnit[0];
                DecPart += Figure[Integer.valueOf(XiaoShu[1]) - 48];
                DecPart += DecUnit[1];
            }

            return IntPart + DecPart;
        }
    }

    public static void main(String[] args) {
        Scanner get = new Scanner(System.in);
        System.out.print("请输入需要转换的金额:");
        double num = get.nextDouble();

        if (num == 0.0) System.out.println("零元整"); //处理特殊情况
        else System.out.println(RMB.toString(num));

        get.close();
    }
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到