中文表达式注意点
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; class NumToExpress{ public static String[] num2Str = { "零","一","二","三","四","五","六","七","八","九","十" }; public static String Bai = "百"; public static String Qian = "千"; public static String Wan = "万"; public static String Yi = "亿"; // 小于9的 public static String num9(int num){ return num2Str[num]; } // 小于99的 public static String num99(int num){ // 十位 个位 int n1 = num / 10; int n2 = num % 10; if(n2 == 0){ // 整除10的 if(n1 == 1){ return num2Str[10]; }else { return num2Str[n1] + num2Str[10]; } }else { if(n1 == 1){ return num2Str[10] + num2Str[n2]; }else { return num2Str[n1] + num2Str[10] + num2Str[n2]; } } } // 小于999 public static String num999(int num){ // 百位 十位 个位 int n1 = num / 100; int n2 = num / 10 - n1 * 10; int n3 = num % 10; if(n2 == 0 && n3 == 0){ // 整百 return num2Str[n1] + Bai; }else if(n2 == 0){ // 几百零几 return num2Str[n1] + Bai + num2Str[0] + num2Str[n3]; }else if(n3 == 0){ // 几百几十 return num2Str[n1] + Bai + num2Str[n2] + num2Str[10]; }else if(n2 == 1) { // 几百一十几 return num2Str[n1] + Bai + num2Str[1] + num99(n2 * 10 + n3); }else { return num2Str[n1] + Bai + num99(n2 * 10 + n3); } } // 小于9999 public static String num9999(int num){ // 千位 百位 十位 个位 int n1 = num / 1000; int n2 = num / 100 - n1 * 10; int n3 = num / 10 - n1 * 100 - n2 * 10; int n4 = num % 10; if(n2 == 0 && n3 == 0 && n4 == 0){ // 整千 return num2Str[n1] + Qian; }else if(n2 == 0){ // 百位为0 if(n3 == 0){ // 十位为0 return num2Str[n1] + Qian + num9(n4); }else{ // 1014:一千零十四, 不用 一千零一十四 // return num2Str[n1] + Qian + num2Str[0] + num2Str[1] + num99(n3 * 10 + n4); return num2Str[n1] + Qian + num2Str[0] + num99(n3 * 10 + n4); } }else { return num2Str[n1] + Qian + num999(num % 1000); } } // 千级别 public static String numQian(int num){ if(num >= 0 && num <= 9){ return num9(num); }else if(num <= 99){ return num99(num); }else if(num <= 999){ return num999(num); }else if(num <= 9999){ return num9999(num); } return null; } // 万级别 public static String numWan(int num) { // 万 int n1 = num / 10000; // 后4位 int n2 = num - n1 * 10000; if(n2 == 0) { // 多少万 return numQian(n1) + Wan; }else { int n2_1 = n2 / 1000; if (n2_1 == 0) { // 千位为0 int n2_2 = n2 / 100 - n2_1 * 10; int n2_3 = n2 / 10 - n2_1 * 100 - n2_2 * 10; if(n2_2 == 0 && n2_3 == 1){ return numQian(n1) + Wan + num2Str[0] + num2Str[1] + numQian(n2 - n2_1 * 1000); } return numQian(n1) + Wan + num2Str[0] + numQian(n2 - n2_1 * 1000); }else { return numQian(n1) + Wan + numQian(n2); } } } // 亿级别 public static String numYi(int num) { int yi = num / 100_000_000; int wan = num - yi * 100_000_000; if(wan == 0){ // 整亿 return num2Str[yi] + Yi; }else { int wan_1 = wan / 10000; if(wan_1 == 0){ return numQian(yi) + Yi + num2Str[0] + numQian(wan); }else{ // 万的首位 int wan_1_1 = wan_1 / 1000; if(wan_1_1 == 0){ return numQian(yi) + Yi + num2Str[0] + numWan(wan); }else { return numQian(yi) + Yi + numWan(wan); } } } } /** * 中文表达式 * @param num * @return */ public static String num2Chinese(int num){ if(num == -2147483648){ return "负二十一亿四千七百四十八万三千六百四十八"; } String s = ""; if(num < 0){ s = "负"; num = -num; } if(num <= 9999){ s += numQian(num); }else if(num <= 9999_9999){ s += numWan(num); }else { s += numYi(num); } return s; } /** * 英文表达式 * @param num * @return */ public static String num2English(int num){ if (num == 0) { return "Zero"; } String res = ""; if (num < 0) { res = "Negative, "; } if (num == Integer.MIN_VALUE) { res += "Two Billion, "; num %= -2000000000; } num = Math.abs(num); int high = 1000000000; int highIndex = 0; String[] names= { "Billion", "Million", "Thousand", ""}; while (num != 0) { int cur = num / high; num %= high; if (cur != 0) { res += num1ToE999(cur); res += names[highIndex] + (num == 0 ? " " : ", "); } high /= 1000; highIndex++; } return res; } static String num1To19(int num) { if (num < 1 || num > 19) { return ""; } String[] names= { "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Sixteen ", "Eighteen ", "Nineteen " }; return names[num - 1]; } static String num1To99(int num) { if (num < 1 || num > 99) { return ""; } if (num < 20) { return num1To19(num); } int high = num / 10; String[] tyNames = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " }; return tyNames[high - 2] + num1To19(num % 10); } static String num1ToE999(int num) { if (num < 1 || num > 999) { return ""; } if (num < 100) { return num1To99(num); } int high = num / 100; return num1To19(high) + "Hundred " + num1To99(num % 100); } } public class Main { public static Scanner sc = new Scanner(System.in); public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { int num = sc.nextInt(); System.out.println(NumToExpress.num2English(num)); System.out.println(NumToExpress.num2Chinese(num)); } }