题解 | #人民币转换# 基本满足所有情况,遗漏请补充

人民币转换

https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static String[] ten = new String[] {
        "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
    };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (sc.hasNext()) {
            String rmb = sc.nextLine();
            System.out.println(solution(rmb));
        }
    }

    //输出人民币格式
    public static String solution(String rmb) {
        String result = "";
        //把整数和小数分隔
        String[] money = rmb.split("\\.");
        if (money[1].equals("00")) {
            result = "人民币" + solveZ(money[0]) + "元整";
        } else if (money[0].equals("0")) {
            result = "人民币" + solveX(money[1]);
        } else {
            result = "人民币" + solveZ(money[0]) + "元" + solveX(money[1]);
        }
        return result;
    }
    //输出整数部分:递归
    public static String solveZ(String str) {
        int money = Integer.parseInt(str);
        String result = "";
        if (money == 0) {
            result = "零";
        } else if (money < 10) {
            //个位
            int temp = money % 10;
            result = ten[temp];
        } else if (money < 100) {
            //个位
            int temp = money % 10;
            //十位
            int shi = money / 10;
            //特殊情况10
            if (shi == 1 && temp == 0) {
                result = "拾";
            }
            //如 20 30
            else if (shi != 1 && temp == 0) {
                result = ten[shi] + "拾";
            }
            //如 15 17 不能有"壹"
            else if (shi == 1 && temp != 0) {
                result = "拾" + ten[temp];
            } else {
                result = ten[money / 10] + "拾" + ten[temp];
            }
        } else if (money < 1000) {
            //十位、个位
            int temp = money % 100;
            if (temp == 0) {
                result = ten[money / 100] + "佰";
            } else if (temp < 10) {
                //如101 中间要添加零
                result = ten[money / 100] + "佰" + "零" + solveZ(String.valueOf(temp));
            } else {
                //如555,递归
                result = ten[money / 100] + "佰" + solveZ(String.valueOf(temp));
            }
        } else if (money < 10000) {
            //百位、十位、个位
            int temp = money % 1000;
            if (temp == 0) {
                result = ten[money / 1000] + "仟";
            } else if (temp < 100) {
                //如1011 1001
                result = ten[money / 1000] + "仟" + "零" + solveZ(String.valueOf(temp));
            } else {
                //如1234 递归
                result = ten[money / 1000] + "仟" + solveZ(String.valueOf(temp));
            }
        } else if (money < 100000000) {
            //如 12567834
            int temp = money % 10000;
            if (temp == 0) {
                result = solveZ(String.valueOf(money / 10000)) + "万";
            } else if (temp < 1000) {
                result = solveZ(String.valueOf(money / 10000)) + "万零" + solveZ(
                             String.valueOf(
                                 temp));
            } else {
                result = solveZ(String.valueOf(money / 10000)) + "万" + solveZ(String.valueOf(
                             temp));
            }
        } else if (money < 1000000000) { //暂时小于10亿
            //如 1 2345 6789
            int temp = money % 100000000;
            if (temp == 0) {
                result = solveZ(String.valueOf(money / 100000000)) + "亿";
            } else {
                result = solveZ(String.valueOf(money / 100000000)) + "亿" + solveZ(
                             String.valueOf(temp));
            }
        }
        return result;
    }

    //输出小数部分
    public static String solveX(String str) {
        String result = "";
        //money 01~99
        int money = Integer.parseInt(str);
        //分
        int fen = money % 10;
        //角
        int jiao = money / 10;
        if (fen == 0) {
            result = ten[jiao] + "角";
        } else if (jiao == 0) {
            result = ten[fen] + "分";
        } else {
            result = ten[jiao] + "角" + ten[fen] + "分";
        }
        return result;
    }
}

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务