【问题标题】:need help converting numbers to word in Java需要帮助在 Java 中将数字转换为单词
【发布时间】:2012-05-14 19:52:06
【问题描述】:

我正在开发一个将数字转换为单词的程序,但是我在 Numbers 类中的 toString() 方法上遇到了问题。所有的方法都给了我,所以我可以实现;因此,我无法删除它们中的任何一个...

编号:4564 -->四千五百六十四

这是代码 数字类

package numberstowords;

import java.util.*;

public class Numbers {

    //array containing single digits words numbers:0-9
    private final String[] SINGLE_DIGITS_WORDS;

    //array containing special words words eg:10-19
    private final String[] TEEN_DIGITS_WORDS;

    //array containing tens words numbers:20-90
    private final String[] TEN_DIGITS_WORDS;

    private int value,   //number to be converted to words
                one,     //number to store digits
                ten,     //number to store tens
                hundred, //number to store hundred
                thousand;//number to store thousand

    private String strOut;

    //conscructor: Initializing value and arrays
    public Numbers(int n)


    //getting single digit number
    private int getOnes()
    {
        one = value % 10;
        return one;
    }

    //getting tens numbers
    private int getTens()
    {
        ten = value % 100;
        ten /= 10;
        return ten;
    }

    //getting hundreds numbers
    private int getHundreds()
    {
        hundred = value % 1000;
        hundred /= 100;
        return hundred;
    }

    //getting thousands numbers
    private int getThousands()
    {
        thousand = value % 10000;
        thousand /= 1000;
        return thousand;
    }

    //converting and returning string of ones 
    private String digitsToString(int one)
    {
        return SINGLE_DIGITS_WORDS[one];        
    }

    //converting and returning strings of tens and teens
    private String tensAndOnesToString(int ten, int one)
    {
        if(ten == 1)//if number is a teen return, else return tens 
        { 
            return TEEN_DIGITS_WORDS[one];
        }
        return TEN_DIGITS_WORDS[ten-2];         
    }

    //converting and returning strings of hundreds
    private String hundredsToString(int hundred)
    {
        return digitsToString(hundred) + " hundred";
    }

    private String thousandsToString(int thousand)
    {
        return digitsToString(thousand) + " thousand";
    }

【问题讨论】:

  • 您的具体问题是什么?试着让你的问题更具体一点。
  • 里面有问题吗?有什么问题?
  • 请添加错误跟踪、错误信息或描述错误情况
  • 使用调试器单步执行代码时发生了什么?
  • 代码本身没有任何错误,它编译得很好,相反我在打印特定数字时遇到问题,例如 11...19 它打印“十一”或“十五”跨度>

标签: java numbers tostring words


【解决方案1】:

根据您的 cmets,问题是您得到了 ones 输出的 11-19 之间的数字。

查看您的tensAndOnesToString() 方法,它检查是否ten == 1,以识别teens 数字。那么,为什么不在if(d4 != 0) 行中进行类似的检查,

    if(d4 != 0 && d3 != 1) // CHANGED THIS LINE
    {
        if(strOut.equals(""))
            strOut = digitsToString(one);
        else
        {
            strOut = strOut +" "+ digitsToString(one);
        }
    }

所以现在它只会输出一个 one 数字,如果它 (d4) 不是 0,并且如果 tens (d3) 不是 1

【讨论】:

    【解决方案2】:

    在这个简短的代码中,我使用了递归函数来使其简单的答案具有良好的性能。 我将给定的数字划分为小块数学可理解的值。 这种类型的划分是基于数学的,主要由 CPU 执行,并且使用较少的内存。

    public class NumbersLetter {
    
        private java.util.HashMap<Long, String> hashMap = new java.util.HashMap<>(34);
        private long[] dividers = {
                1_000_000_000_000_000_000L, 1_000_000_000_000_000L, 1_000_000_000_000L,
                1_000_000_000L, 1_000_000L, 1_000L, 100L, 10L, 1L};
    
        public String getNaturalNumbersLetter(long number) {
            String regex = "[0-9]+";
            if (!Long.toString(number).matches(regex)) {
                number = 0;
            }
            return divideNumber(number);
        }
    
        private String getDigitLetter(long digit) {
            return hashMap.get(digit);
        }
    
        private String addSeparation(boolean addSeperator) {
            if (addSeperator) return " and ";
            return "";
        }
    
        private String divideNumber(long digit) {
            //Check if the number is a pure number
            // and mapped in our @hashMap
            if (hashMap.containsKey(digit)) {
                return " " + getDigitLetter(digit);
            }
            // Start to divide the given number
            // to small pieces of math understandable values
            // This type of divide is math based
            // which do mostly by the CPU and
            // use small amount of RAM.
            for (long i : dividers) {
                /**
                 * Start divide the given number to smaller pieces
                 * for example will change 4,321 to
                 * 4000 , 300 ,20 & 1
                 * this is one of those formats which is human readable.
                 * The important thing about this design is that
                 * I use calculation instead of buffering strings.
                 * */
                if ((digit >= i)) {
                    if (digit % i == 0) {
                        //
                        return divideNumber(digit / i) + divideNumber(i);
                    } else {
                        if ((digit / i) == 1) {
                            return divideNumber(digit / i)
                                    + divideNumber((digit / i) * i)
                                    + divideNumber(digit % i);
                        } else {
                            return divideNumber((digit / i) * i)
                                    + divideNumber(digit % i);
                        }
                    }
                }
            }
            return "";
        }
    
        public NumbersLetter() {
            // NumbersLetter Constructor
            hashMap.put(0L, "Zero");
            hashMap.put(1L, "One");
            hashMap.put(2L, "Two");
            hashMap.put(3L, "Three");
            hashMap.put(4L, "Four");
            hashMap.put(5L, "Five");
            hashMap.put(6L, "Six");
            hashMap.put(7L, "Seven");
            hashMap.put(8L, "Eight");
            hashMap.put(9L, "Nine");
            hashMap.put(10L, "Ten");
            hashMap.put(11L, "Eleven");
            hashMap.put(12L, "Twelve");
            hashMap.put(13L, "Thirteen");
            hashMap.put(14L, "Fourteen");
            hashMap.put(15L, "Fifteen");
            hashMap.put(16L, "Sixteen");
            hashMap.put(17L, "Seventeen");
            hashMap.put(18L, "Eighteen");
            hashMap.put(19L, "Nineteen");
            hashMap.put(20L, "Twenty");
            hashMap.put(30L, "Thirty");
            hashMap.put(40L, "Forty");
            hashMap.put(50L, "Fifty");
            hashMap.put(60L, "Sixty");
            hashMap.put(70L, "Seventy");
            hashMap.put(80L, "Eighty");
            hashMap.put(90L, "Ninety");
            hashMap.put(100L, "Hundred");
            hashMap.put(1_000L, "Thousand");
            hashMap.put(1_000_000L, "Million");
            hashMap.put(1_000_000_000L, "Billion");
            hashMap.put(1_000_000_000_000L, "Trillion");
            hashMap.put(1_000_000_000_000_000L, "Quadrillion");
            hashMap.put(1_000_000_000_000_000_000L, "Quintillion");
        }
    
    }
    

    【讨论】:

    • 好的答案通常会解释他们如何解决所提问题的核心问题。您可能希望在此代码前加上您为解决问题所做的总体思路。
    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多