package com.wang.demo; import java.util.Scanner; //金额转换 public class MyTest3 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("请输入你想要转换的数字"); int num = sc.nextInt(); System.out.println(CashReturn(num)); } public static final String[] numberChinese={ "壹","贰","叁","肆","伍","陆","柒","捌","玖" }; public static String CashReturn(int num){ if (num==0){ String result = "零佰零拾零万零仟零佰零拾零元"; return result; } int numLength =String.valueOf(num).length(); String[] number = {"零","佰","零","拾","零","万","零","仟","零","佰","零","拾","零","元"}; if (numLength> number.length/2){ String result = "您输入的数字超过了可转换金额范围,在此提醒可转换金额最高为百万下"; return result; } String[] numberChina = new String[numLength]; String numberChange = Integer.toString(num); for (int i = 0,b=0; i < numberChange.length(); i++,b++) { int a= Integer.parseInt(String.valueOf(numberChange.charAt(b))); //这里把输入的数字转化为中文版的数字,下一步数组里的元素互换 numberChina[i] = numberChinese[a-1]; } for (int i = numberChina.length-1,b=2; i >=0; i--,b+=2) { int a =number.length-b; number[a]=numberChina[i]; } String result = String.join("", number); return result; } }
java金额转换
最新推荐文章于 2024-09-04 15:29:36 发布