java - 编写更短的 if else 代码

标签 java

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 语句来改进我的代码编写。有什么方法可以改进这段代码,使其变得更短并且像专业人士一样可读?

最佳答案

希望对你有帮助

    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;

关于java - 编写更短的 if else 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705286/

相关文章:

java - JSP简单插入无法在类中运行

java - 我正在尝试这个简单的数独

java - 如何使用自定义类加载器到 Java 中的新对象

java - 如何在 JDK8 上使用 SPDY 运行 Jetty?

Java :Is Set not allowed duplicates even within List object?

java - 如何从 Java 中的序列化对象加载选择性数据?

java - 通过@ManagedProperty 检索更新的值/通知其他 beans 属性的更新值

java - 我是否使用正确的代码来获取文本文件中注册页面的输出

java - 框架如何调用 javabean 上的 get 和 set 方法?

java - 在字符串链表的链表中查找重复项的最有效方法 - java