Java学习---金额转换(用字符串数组)

金额最多七位

例:2135----->零佰零拾零万贰仟壹佰叁拾伍元

import java.util.Scanner;

public class AmountConversion2 {
    public static void main(String[] args) {
        //输入金额
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入不超过7位数字:");
        //循环直到键盘输入的是不超过7位数字
        while (true){
            int money = sc.nextInt();
            if (money>=0&&money<=9999999) {
                System.out.println("转换结果为:");
                System.out.println(split2(split1(split(money))));
                break;
            }else {
                System.out.println("输入的数字超过7位,请重新输入:");
            }
        }
    }
    //小写数字大写(2135转化为贰壹叁伍)
    public static String split(int money){
        String[] arr ={"零","壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌","玖"};
        String  result = "";
        while (money != 0){
            int ge = money % 10;
            result = arr[ge] + result;
            money= money / 10;

        }
        return result;
    }
    //贰壹叁伍转化为零零零贰壹叁伍
    public static String split1(String sh){
        String result =sh;
        int count = 7-result.length();
        for (int i = 0; i < count; i++) {
            result = "零" + result;
        }
        return result;
    }
    //零零零贰壹叁伍转化为零佰零拾零万贰仟壹佰叁拾伍元
    public static String split2(String  sh){
        String[] arr ={"佰","拾","万","仟","佰","拾","元"};
        String result = "";
        for (int i = 0; i < sh.length(); i++) {
            result = result + sh.charAt(i) + arr[i];
        }
        return result;
    }
}

运行结果

请输入不超过7位数字:
11111111
输入的数字超过7位,请重新输入:
2135
转换结果为:
零佰零拾零万贰仟壹佰叁拾伍元

进程已结束,退出代码0
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值