Java實現人民幣大小寫轉換

思路和具體方法都在註釋中說明

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("元");
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章