将数字更改为单词时如何实现青少年逻辑

How to implement teen logic when changing numbers to words

我只是想弄清楚如何为这段代码实现青少年逻辑。 例如: 如果用户输入 115 输出应该是 One hundred and fifteen。 相反,我的输出是 One hundred and ten five.

任何人都知道如何解决这个问题

public class NumberToWords {
public static void main(String[] args) {

  String [] units = {"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","ninteen "};
  String tens[]=     {"","ten ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
  String hundreds =   "hundred ";

  System.out.print("Number: ");
  int num = In.nextInt();
    
  while (num != -1) {
   int tt = num/10;
   int t  = tt%10;
   
   if (num == 0){
           System.out.print("zero ");
        }

   if(num<20){//till 19
            System.out.println(units[num]);
        }
        else if(num<100){//till 99
            System.out.println(tens[num/10] + ((num%10!=0) ?"":"") + units[num%10]);
         }
        else if (num<1000){//till 999 542
          System.out.println(units[num/100]+hundreds+((num%100!= 0)?"and ":"")+tens[t]+units[num%10]);
           
        }
       
        System.out.print("Number: ");
    num = In.nextInt();

 }

  if (num == -1) {
          System.out.println("Done");
        }
 }

}

您正在将每个数字作为一个单独的数字进行计算并按此打印。您的程序需要通过创建一个像这样的新函数来处理 10-19 的特殊情况:

// Expected that val is 0 - 99
public static String get_correct_val(int val)
{
    String [] units = {"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "}
    String [] specials = {"","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","ninteen "};
    String tens[]=     {"","ten ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};

    if(val < 10)
    {
        return units[val];
    }
    else if(val < 20)
    {
        return specials[val - 9];
    }
    else
    {
        return tens[Math.floor(val/10) % 10] + units[val % 10];
    }
}

现在您可以从主函数中调用它了:

        if(num<20){//till 19
            System.out.println(units[num]);
        }
        else if(num<100){//till 99
            System.out.println(tens[num/10] + get_correct_val(num % 100));
        }
        else if (num<1000){//till 999 542
            System.out.println(units[num/100]+hundreds+((num%100!= 0)?"and ":"") + get_correct_val(num % 100));
        
        }

我尝试了一种快速的不同方法,该方法仅限于从 0 到 999 的工作。

import java.util.HashMap;
import java.util.Map;

import java.util.Scanner;

public class numberWord {
    public static Map<Integer, String> map = new HashMap<>();
public static void main(String[] args) {

    map.put(0, "zero");
    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, "nineteen");
    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, "ninty");

    Scanner sc = new Scanner(System.in);
    int inp = sc.nextInt();

    if (inp > -1 && inp < 1000) {
        compute(inp);

    } else {
        System.out.println("Out of scope");
    }

    sc.close();

}

public static void compute(int inp) {
    if (inp <= 20) {
        System.out.println(" " + map.get(inp));
    } else if (inp > 20 && inp <= 99) {
        int div = inp / 10;
        int rem = inp % 10;
        System.out.print(" " + map.get(div * 10));
        if (rem != 0) {
            System.out.print(" " + map.get(rem));
        }

    } else {
        int div = inp / 100;
        int rem = inp % 100;
        System.out.print(map.get(div) + " hundred");
        if (rem != 0) {
            compute(rem);
        }
    }
}

}