1、题目
Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
说明:
数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。
方法原型:public static String parse(long num)
2、这题目思路很简单,但是比较折腾人,很容易犯错。我看到题目的第一思路就是用个hashMap把不规则数字英语单词和对应的数字保存起来,然后根据输入的数字,根据个十百千万…位来取数字,然后加上billion、million、thousand、hundred and等词,由于英语数字有规律,3个为一组,可以加以利用;而且数字进行重复处理比较多,比如123,和23,它们都要输出23,所以最好写成函数来避免重复工作;还有就是我将英语数字1~20分成一组,为什么这样分因为前20个数没什么规律,比如twelve,而之后的数都有规律比如twenty one。import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); if(str.matches("^[1-9][0-9]*$")){ long n = Long.parseLong(str); System.out.println(parse(n)); }else{ System.out.println("error"); } scanner.close(); } public static String parse(long num){ HashMap<String,String> map = new HashMap<String,String>(); String str = ""; map.put("1", "one"); map.put("2", "two"); map.put("3", "three"); map.put("4", "four"); map.put("5", "five"); map.put("6", "six"); map.put("7", "seven"); map.put("8", "eight"); map.put("9", "nine"); map.put("10", "ten"); map.put("11","eleven"); map.put("12", "twelve"); map.put("13", "thirteen"); map.put("14", "fourteen"); map.put("15", "fifteen"); map.put("16", "sixteen"); map.put("17", "seventeen"); map.put("18", "eighteen"); map.put("19", "nighteen"); map.put("20", "twenty"); map.put("30", "thirty"); map.put("40", "forty"); map.put("50", "fifty"); map.put("60", "sixty"); map.put("70", "seventy"); map.put("80", "eighty"); map.put("90", "ninety"); if(num <= 20){ str = getOne(map,num); }else if(num < 100){ str = getTwo(map,num); }else if(num<1000){ str = getThree(map,num); }else if(num <= 20000){ str = getFour(map,num); }else if(num < 100000){ str = getFive(map, num); }else if(num < 1000000){ str = getSix(map, num); }else if(num <= 20000000){ str = getSeven(map, num); }else if(num < 100000000){ str = getEight(map,num); }else if(num < 1000000000){ str = getNine(map, num); }else if(num <= 2147483647){ str = getTen(map, num); }else{ str = "error"; } return str; } public static String getOne(HashMap<String,String> map,long num){ if(num == 0){ return ""; }else return map.get(num+""); } public static String getTwo(HashMap<String,String> map,long num){ if(num == 0){ return ""; }else if(num/10 == 0){ return getOne(map, num%10); }else return getOne(map,((num/10)*10))+ " " + getOne(map, num%10); } public static String getThree(HashMap<String,String> map,long num){ // 如果高位连续3位小于100,则必须以他们自身值作为百位,无需再除以100 if(num/100 <= 0){ if(num == 0){ return ""; }else if(num <= 20){ return "and "+getOne(map,num); }else{ return "and "+getTwo(map, num); } } else if(num % 100 == 0){ return getOne(map,num/100) +" hundred"; }else if(num % 100 < 20){ return getOne(map,num/100) +" hundred and "+ getOne(map,num%100); }else{ return getOne(map,num/100) +" hundred and "+getTwo(map,num%100); } } public static String getFour(HashMap<String,String> map,long num){ if(num/1000 == 0){ return getThree(map,num%1000); }else return getOne(map,num/1000) + " thousand " + getThree(map,num%1000); } public static String getFive(HashMap<String,String> map,long num){ if(num/1000 == 0){ return getThree(map,num%1000); }else return getTwo(map,num/1000)+" thousand " + getThree(map,num%1000); } public static String getSix(HashMap<String,String> map,long num){ if(num/1000 == 0){ return getThree(map,num%1000); }else return getThree(map,num/1000) +" thousand "+getThree(map,num%1000); } public static String getSeven(HashMap<String,String> map,long num){ if(num/1000000 == 0){ return getSix(map, num%1000000); }else return getOne(map, num/1000000) +" million "+getSix(map, num%1000000); } public static String getEight(HashMap<String,String> map,long num){ if(num/1000000 == 0){ return getSix(map, num%1000000); }else return getTwo(map,num/1000000)+" million "+getSix(map, num%1000000); } public static String getNine(HashMap<String,String> map,long num){ if(num/1000000 == 0){ return getSix(map, num%1000000); }else return getThree(map, num/1000000)+" million "+getSix(map, num%1000000); } public static String getTen(HashMap<String,String> map,long num){ return getOne(map, num/1000000000) +" billion " + getNine(map, num%1000000000); } }