需要帮助在Java中将数字转换为单词 [英] need help converting numbers to word in Java

查看:84
本文介绍了需要帮助在Java中将数字转换为单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a program that converts numbers to words, but I'm having problems with the toString() method in the Numbers class. All the methods were given to me, so I could implement; therefore, I can't remove any of them...

数字:4564 - >四千五百六十四

number: 4564 --> four thousand and five hundred and sixty four

这里是代码
Numbers class

here's the code Numbers class

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 之间的数字的那些输出。

According to your comments, the problem is that you're getting the ones output for numbers between 11-19.

查看 tensAndOnesToString()方法,检查 ten == 1 ,用于识别青少年号码。所以,为什么不在你的中进行类似的检查if(d4!= 0)行,

Looking at your tensAndOnesToString() method, it checks whether ten == 1, for the purpose of identifying the teens numbers. So, why don't you put a similar check in your if(d4 != 0) line,

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

所以现在只输出一个号码,如果它( d4 )不是 0 ,如果 tens d3 )不是 1

So now it will only output a one number if it (d4) isn't 0, and if the tens (d3) isn't 1.

这篇关于需要帮助在Java中将数字转换为单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆