Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
说明:
数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。
方法原型:public static String parse(long num)
输入一个long型整数
输出相应的英文写法
2356
two thousand three hundred and fifty six
import java.util.Scanner;
public class Main { public static void main(String[] args) { // System.out.println(Long.MAX_VALUE); //long max:9223372036854775807 // System.out.println("int :" + Integer.MAX_VALUE); //int max:2147483647 Scanner scan = new Scanner(System.in); while (scan.hasNext()) { String line = scan.nextLine(); //去除两端的空格操作 String str = line.trim(); //判断输入条件,如果过滤后输入的字符串长度为0,输出error if (str.length() == 0) { System.out.println("error"); continue; } //判断字符串是否是纯数字,true表示不是数字 boolean flag = true; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) < '0' || str.charAt(i) > '9') { System.out.println("error"); flag = false; break; } } //如果不是纯数字,继续下一次输入字符串 if (!flag) { continue; } //字符串转化为长整型数字 long N = Long.parseLong(str); String final_str = ""; //判断输入是否合法,正整数,长度不超过十位 if (isLegal(N)) { final_str = parse(N); System.out.println(final_str); } else { System.out.println("error"); } } scan.close(); } /** * 判断输入是否合法,数字为正整数,长度不超过十位 * */ private static boolean isLegal(long n) { boolean flag = false; String limit_str = "9999999999"; long limit = Long.parseLong(limit_str); if (n <= limit && n >= 0) { flag = true; } return flag; } /** * 将数字翻译成英文写法 * @param n是用户输入的长整型数字 * */ public static String parse(long n) { String str = ""; // count_n代表数字的位数 int count_n = getDigits(n); int temp_count_n = count_n;// 每次减一,寻找低的量级 int index_number = 0;// 保存某一位上的数字 for (int i = 1; i <= count_n; i++) { // 获取某一位上的数字 index_number = getPositionInt(n, count_n, i); str += matchEnglishKey(temp_count_n--, index_number , n , count_n , i); } return str; } /** * 获得整数某一位上的数字 * @param n是用户输入的原始长整型数字 * @param count_n是数字n的总的长度 * @param index代表数字n中从左向右数第几个数字 * */ public static int getPositionInt(long n, int count_n, int index) { // System.out.println("n:" + n + ",count_n:" + count_n + ",index:" + index); //向前寻找的时候如果index越界,直接返回0,避免系统报错 if(index < 1) return 0; // //向后寻找的时候如果index越界,直接返回0 // if(index > count_n) return 0; int number = 0; int count_time = count_n - index;// 统计寻找的次数 // System.out.println("需要计算的次数:" + count_time); while (count_time > 0) { n = n / 10; count_time--; } number = (int) (n % 10); return number; }
/** * 英文字母匹配,获得它的单位量级,billion,million,thousand,hundred * @param n代表当前数字的总的位数,在数字总位数的基础上每次减一,根据位数可以判断出来单位量级 * @param index某位置上的数字,如果某位置上的数字为0需要特殊处理 * @param number代表用户输入的长整型数字 * @param count_n代表原始数字总的位数 * @param i位置的数字 * */ public static String matchEnglishKey(int n, int index , long number , int count_n , int i) { String str = ""; int index_number_after = 0; int index_number_after2 = 0; int index_number_before = 0; int index_number_before2 = 0; //n当前数字总的位数 switch (n) { //max_n 9999999999 表示 9 billion 999 million 999 thousand 999 case 10: //如果n=10,则index一定非0 str += matchBitInt(index); str += " billion"; break; case 9: //index某位置上的数字 为0时候,不输出 if(index == 0){ str += ""; }else{ //拼接100~999million语句 分为百位 十位 个位 //十位 index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 //个位 index_number_after2 = getPositionInt(number, count_n, i + 2);//保存当前位后面再后面一位的数字 // System.out.println("i:" + i +",after:" + index_number_after + ",after2:" + index_number_after2); //百位后面两位都为0 if(index_number_after == 0 && index_number_after2 == 0 ){ if(count_n == n){ str += matchBitInt(index); str += " hundred"; }else{ str += " "; str += matchBitInt(index); str += " hundred"; } }else{ //后面两位如果有一位不为0都要添加and连接 // 在百位后面加and if(count_n == n){ str += matchBitInt(index); str += " hundred and "; }else{ str += " "; str += matchBitInt(index); str += " hundred and "; } } } break; case 8: index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 //index某位置上的数字 为0时候,不输出 if(index == 0){ str += ""; }else{ //拼接10~99million语句 分为十位 个位 10~19 20~99 //如果当前位置上的数字等于1,10~19 if(index == 1){ //10~19 str += matchTenToNineteen(index , index_number_after); str += ""; }else{ //>=20 if(index_number_after != 0){ //非整数21 ....,需要拼接 str += matchTenInt(index); str += " "; }else{ //整数20 30 40 ... 90 str += matchTenInt(index); } } } break; case 7: //拼接0~9million语句 index_number_before = getPositionInt(number, count_n, i - 1);//保存当前位前一位的数字 index_number_before2 = getPositionInt(number, count_n, i - 2);//保存当前位前两位的数字 if(index == 0){ //当前位置的字符为0 if(index_number_before >= 1){ //如果前一位大于等于1 str += " million"; }else{ //如果前一位等于0,看前两位 if(index_number_before2 == 0){ str += ""; }else{ //如果前两位不等于0 str += " million"; } } }else{ //当前位置的字符不等于0 // System.out.println("index_number_before" + index_number_before); //如果前面的数字等于1,代表10~19表示已经输出过了 if(index_number_before != 1){ //如果前面的数不等于1,并且当前位置的数字不等于0 str += matchBitInt(index); str += " million"; }else{ str += " million"; } } break; case 6: if(index == 0){ str += ""; }else{ //拼接100~999thousand语句 分为百位 十位 个位 //十位 index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 //个位 index_number_after2 = getPositionInt(number, count_n, i + 2);//保存当前位后面再后面一位的数字 // System.out.println("i:" + i +",after:" + index_number_after + ",after2:" + index_number_after2); //后面两位如果有有一位不为0都要添加and连接 if(index_number_after == 0 && index_number_after2 == 0){ //百位后面两位都为0 //如果数字本来就是6位 if(count_n == n){ str += matchBitInt(index); str += " hundred"; }else{ str += " "; str += matchBitInt(index); str += " hundred"; } }else{ // 在百位后面加and if(count_n == n){ str += matchBitInt(index); str += " hundred and "; }else{ str += " "; str += matchBitInt(index); str += " hundred and "; } } } break; case 5: index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 if(index == 0){ str += ""; }else{ //如果当前位置上的数字等于1,,10~19 if(index == 1){ //10~19 str += matchTenToNineteen(index , index_number_after); str += ""; }else{ //>=20 if(index_number_after != 0){ str += matchTenInt(index); str += " "; }else{ str += matchTenInt(index); } } } break; case 4: index_number_before = getPositionInt(number, count_n, i - 1);//保存当前位前一位的数字 index_number_before2 = getPositionInt(number, count_n, i - 2);//保存当前位前两位的数字 //前面有1个数字 21,1是当前位置的字符 if(index == 0){ //当前位置的字符为0 if(index_number_before >= 1){ //如果前一位大于等于1 str += " thousand"; }else{ //如果前一位等于0,看前两位 if(index_number_before2 == 0){ str += ""; }else{ //如果前两位不等于0 str += " thousand"; } } }else{ //当前位置的字符不等于0 // System.out.println("index_number_before" + index_number_before); //如果前面的数字等于1,代表10~19表示已经输出过了 if(index_number_before != 1){ //如果前面的数不等于1,并且当前位置的数字不等于0 str += matchBitInt(index); str += " thousand"; }else{ str += " thousand"; } } break; case 3: index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 index_number_after2 = getPositionInt(number, count_n, i + 2);//保存当前位后面再后面一位的数字 if(index == 0){ str += ""; }else{ // System.out.println("i:" + i +",after:" + index_number_after + ",after2:" + index_number_after2); //后面两位如果有有一位不为0都要添加and连接 if(index_number_after == 0 && index_number_after2 != 0 || index_number_after != 0 && index_number_after2 == 0 || index_number_after != 0 && index_number_after2 != 0){ // 在百位后面加and if(count_n == n){ str += matchBitInt(index); str += " hundred and "; }else{ str += " "; str += matchBitInt(index); str += " hundred and "; } } if(index_number_after == 0 && index_number_after2 == 0 ){ //百位后面两位都为0 if(count_n == n){ str += matchBitInt(index); str += " hundred"; }else{ str += " "; str += matchBitInt(index); str += " hundred"; } } } break; case 2: index_number_after = getPositionInt(number, count_n, i + 1);//保存当前位后一位的数字 index_number_before = getPositionInt(number, count_n, i - 1);//保存当前位后一位的数字 if(index == 0){ str += ""; }else{ if(index == 1){ str += matchTenToNineteen(index , index_number_after); str += ""; }else{ if(index_number_after != 0){ if(index_number_before == 0){ str += " "; str += matchTenInt(index); str += " "; }else{ str += matchTenInt(index); str += " "; } }else{ str += matchTenInt(index); } } } break; case 1: index_number_before = getPositionInt(number, count_n, i - 1);//保存当前位后一位的数字 //前面有1个数字 21,1是当前位置的字符 if(index == 0){ str += ""; }else{ //当前位置的字符不等于0,判断十位,百位的数字 // System.out.println("index_number_before" + index_number_before); //如果前面的数字等于1,代表10~19表示已经输出过了 if(index_number_before != 1){ //如果前面的数不等于1,并且当前位置的数字不等于0 str += matchBitInt(index); }else{ str += ""; } } break; default: break; } return str; }
/** * 匹配个位数,1,2... * @param n是个位上的数字 * */ public static String matchBitInt(int n) { String str = ""; switch (n) { case 9: str = "nine"; break; case 8: str = "eight"; break; case 7: str = "seven"; break; case 6: str = "six"; break; case 5: str = "five"; break; case 4: str = "four"; break; case 3: str = "three"; break; case 2: str = "two"; break; case 1: str = "one"; break; default: str = ""; break; } return str; }
/** * 匹配10~19这十个数字 * */ public static String matchTenToNineteen(int shiWei , int geWei){ String str = ""; int n = shiWei * 10 + geWei; switch(n){ case 10: str += "ten"; break; case 11: str += "eleven"; break; case 12: str += "twelve"; break; case 13: str += "thirteen"; break; case 14: str += "fourteen"; break; case 15: str += "fifteen"; break; case 16: str += "sixteen"; break; case 17: str += "seventeen"; break; case 18: str += "eighteen"; break; case 19: str += "nineteen"; break; default: str += ""; break; } return str; } /** * 匹配十位数,20,30... * */ public static String matchTenInt(int n) { String str = ""; switch (n) { case 9: str = "ninety"; break; case 8: str = "eighty"; break; case 7: str = "seventy"; break; case 6: str = "sixty"; break; case 5: str = "fifty"; break; case 4: str = "forty"; break; case 3: str = "thirty"; break; case 2: str = "twenty"; break; default: str = ""; break; } return str; }
/** * 获取输入整数的位数 * */ private static int getDigits(long n) { int count = 0; while (n != 0) { count++; n = n / 10; } // System.out.println(count); return count; } }