public class NumberToWords2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 30001;
numberToWords(n);
}
public static String numberToWords(int n){
String temp = Integer.toString(n);
int[] myArr = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
myArr[i] = temp.charAt(i) - '0';
}
if(myArr.length == 1){
System.out.println(oneDigits(n));
}
else if(myArr.length == 2){
System.out.println(twoDigits(n));
}
else if(myArr.length == 3){
System.out.println(threeDigits(n));
}
else if(myArr.length == 4){
System.out.println(fourDigits(n));
}
else if(myArr.length == 5){
System.out.println(fiveDigits(n));
}
return "Invalid Input";
}
///// Methods to return the equivalent English words. Logic and "And" "Thousands" etc /////
private static String fiveDigits(int n) {
// TODO Auto-generated method stub
//Check for 20000, 30000, 40000 etc
if(n%1000 == 0){
return twoDigits(n/1000) + " Thousand";
}
//Numbers starting with 1
if(n/10000 == 1){
//Handle numbers like 10001, 10002, 70024, 80099 etc
if(n%1000 < 100){
return ones(n/1000) + " Thousand And " + twoDigits(n%1000);
}
else{
return ones(n/1000) + " Thousand " + threeDigits(n%1000);
}
}
else{
if(n%1000 < 100){
return twoDigits(n/1000) + " Thousand And " + twoDigits(n%1000);
}else{
return twoDigits(n/1000) + " Thousand " + threeDigits(n%1000);
}
}
}
private static String fourDigits(int n) {
// TODO Auto-generated method stub
//Check for 2000, 3000, 4000 etc
if(n%1000 == 0){
return ones(n/1000) + " Thousand";
}
//Handle numbers like 1001, 1002, 7024, 8099 etc
else if(n%1000 < 100){
return ones(n/1000) + " Thousand And " + twoDigits(n%1000);
}
//Normal Case
else{
return ones(n/1000) + " Thousand " + threeDigits(n%1000);
}
}
private static String threeDigits(int n) {
// TODO Auto-generated method stub
//Check for 200, 300, 400 etc
if(n%100 == 0){
return ones(n/100) + " Hundred";
}
//Normal Case
else{
return ones(n/100) + " Hundred And " + twoDigits(n%100);
}
}
private static String twoDigits(int n) {
// TODO Auto-generated method stub
//Check for 11, 12, 13, 14 etc OR Handle Single digit so can reuse code
if(n/10 == 1 || n/10 == 0)
return ones(n);
//Check for 20, 30, 40 etc. Cannot print zero at the back
else if(n%10 == 0){
return tens(n/10);
}
//Normal Case
else{
return tens(n/10) + " " + ones(n%10);
}
}
private static String oneDigits(int n) {
// TODO Auto-generated method stub
return ones(n);
}
///// Return number words only /////
private static String ones(int num){
Map<Integer, String> h = new HashMap<Integer, String>();
h.put(0 , "Zero");
h.put(1 , "One");
h.put(2 , "Two");
h.put(3 , "Three");
h.put(4 , "Four");
h.put(5 , "Five");
h.put(6 , "Six");
h.put(7 , "Seven");
h.put(8 , "Eight");
h.put(9 , "Nine");
h.put(10, "Ten");
h.put(11 , "Eleven");
h.put(12 , "Twelve");
h.put(13 , "Thirteen");
h.put(14 , "Fourteen");
h.put(15 , "Fifteen");
h.put(16 , "Sixteen");
h.put(17 , "Seventeen");
h.put(18 , "Eighteen");
h.put(19 , "Nineteen");
return h.get(num);
}
private static String tens(int num){
Map<Integer, String> h = new HashMap<Integer, String>();
h.put(2 , "Twenty");
h.put(3 , "Thirty");
h.put(4 , "Fourty");
h.put(5 , "Fifty");
h.put(6 , "Sixty");
h.put(7 , "Seventy");
h.put(8 , "Eighty");
h.put(9 , "Ninety");
return h.get(num);
}
}
我试图学习使用更简单,更优雅的方法来改进我的代码编写的方法来执行条件if else语句。有没有什么方法可以改进这个代码,使其像专业人士一样更短,更易读?
答案 0 :(得分:1)
我希望它可以帮到你
int numOfDigits = n/1000;
String result = String.valueOf(numOfDigits == 1? ones(numOfDigits) : twoDigits(numOfDigits));
String resultString = result + " Thousand " + twoDigits(n%1000);
if(n%1000 < 100){
resultString = result + " Thousand And " + threeDigits(n%1000);
}
return resultString;
答案 1 :(得分:0)
不知道你是否认为这更好,但这里有一个建议:(完全未经测试,但你明白了)
String baseStr;
if(n/10000 == 1) {
//Handle numbers like 10001, 10002, 70024, 80099 etc
baseStr = ones(n/1000);
} else {
baseStr = twoDigits(n/1000);
}
return baseStr + " Thousand " + ((n%1000 < 100) ? "And " twoDigits(n%1000) : + threeDigits(n%1000));
答案 2 :(得分:0)
我猜你可以像下面那样做空。
if(n/10000 == 1 && n%1000 < 100) {
return ones(n/1000) + " Thousand And " + twoDigits(n%1000);
}else if(n%1000 < 100) {
return twoDigits(n/1000) + " Thousand And " + twoDigits(n%1000);
}else{
return twoDigits(n/1000) + " Thousand " + threeDigits(n%1000);
}
答案 3 :(得分:0)
您可以通过重构逻辑并逐个构建结果来获得更简单的代码:
String result = "";
if (n/10000 == 1) {
//Handle numbers like 10001, 10002, 70024, 80099 etc
result += ones(n/1000) + " Thousand";
} else {
result += twoDigits(n/1000) + " Thousand";
}
if (n%1000 < 100) {
result += " And " + twoDigits(n%1000);
} else {
result += " " + threeDigits(n%1000);
}
return result;
答案 4 :(得分:0)
您可以使用两个三元运算符使其成为单个表达式。
为了便于阅读,您还可以将n / 1000
和n % 1000
的值存储在局部变量中,以避免重复。
int q = n / 1000;
int r = n % 1000;
return (q / 10 == 1 ? ones(q) : twoDigits(q)) + " Thousand "
+ (r < 100 ? "And " + twoDigits(r) : threeDigits(r));
答案 5 :(得分:0)
您可以保存计算,提供有意义的名称并正确缩进代码,如下所示:
int nOverThousand = n / 1000;
int nPercThousand = n % 1000;
int twoDigitsTerm = twoDigits(nPercThousand);
int threeDigitsTerm = threeDigits(nPercThousand);
if(nOverThousand / 10 == 1){
//Handle numbers like 10001, 10002, 70024, 80099 etc
int term1 = ones(nOverThousand);
return (nPercThousand < 100) ?
(term1 + " Thousand And " + twoDigitsTerm) :
(term1 + " Thousand " + threeDigitsTerm);
}
else{
int term1 = twoDigits(nOverThousand);
return (nPercThousand < 100) ?
(term1 + " Thousand And " + twoDigitsTerm) :
(term1 + " Thousand " + threeDigitsTerm);
}
请注意,