金额转换1

`
package org.example;

import java.util.Scanner;

public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money;
while (true) {
System.out.println("请输入一个金额");
money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
break;
}
else {
System.out.println("金额无效");
}

    }
    String moneystr="";
    while(true)
    {
        int ge=money%10;
        String capitalNumber=getCapitalNumber(ge);
        moneystr=capitalNumber+moneystr;
        money=money/10;
        if(money==0)
        {
            break;
        }
    }
    int count=7-moneystr.length();
    for (int i = 0; i < count; i++) {
       moneystr= "零"+ moneystr;

    }
    System.out.println(moneystr);
    String[] arr={"佰","拾","万","仟","佰","拾","元"};
    String result="";
   for(int i=0;i<moneystr.length();i++)
   {
       char ch=moneystr.charAt(i);
      result=result+ch+arr[i];

   }

    System.out.println(result);

}

//把数字变成大写
public static String getCapitalNumber(int number)
{
    String[] arr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    return arr[number];
}

}

`

posted @ 2025-01-15 17:20  ytr123  阅读(34)  评论(0)    收藏  举报