package Text2; import java.util.Scanner; public class Stringdemo1 { 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 getnumbercapital = getnumbercapital(ge); // 字符拼接 moneystr= getnumbercapital+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 c = moneystr.charAt(i); // 打印moneystr里面的内容 // System.out.print(c); // 把arr中单位插入数字后面 // System.out.print(arr[i]); result = result + c + arr[i]; } // 打印result System.out.println(result); } // 定义小写转为大写的方法 public static String getnumbercapital(int number){ String []arr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖",}; return arr[number]; } }
Java 字符串练习-----金额转换
最新推荐文章于 2024-07-30 20:50:32 发布