我想帮助显示1到1百万的完整数字

时间:2015-03-26 16:32:12

标签: java

我被赋予了一项任务,我不知道该怎么做,我一直在编写我的程序并且达到了我无法继续的地步,我的问题可能很简单,对你们有些人人们,但不是真的我,我需要一种方法来列出1到100万的数字,但在单词中,例如(一二三四.......)我不能找到一种方法这样做除了设置每一个我需要想象的是,你可以使用for循环而不是我只需要System.out.pritnln来显示它。谁能帮我这个?

3 个答案:

答案 0 :(得分:0)

多年前我做过这样的事情,很久以后甚至不记得哪种语言。

我认为我使用Case语句来表示一位和两位数。有点痛苦,但你只需要写一次这个函数。

基本上你需要你的一到十九,这有点奇怪的解析。然后你的二十,三十到九十的单个数字。其余的很简单,数百,数百万。

您将转换为字符串并获取字符串的长度,并简单地剥去模3的起始字符。例如,1234是四个字符,因此数千,解析第一个(4%3 = 1)字符并在case语句中解码。然后234将是Hundred,得到第一个字符的情况(Two),留下34.第一个字符

这是预先caffine,还不是很清醒(LOL),所以我希望这有帮助...

答案 1 :(得分:0)

这是另一个适合你的,不是最优雅的,但应该给你更多的想法......

public class WriteNum {

    private static final String[] NUMBERS = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    private static final String[] TEN_NUMBERS = { "", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "nintey" };
    private static final String[] TEEN_NUMBERS = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
            "ninteen" };

    public static void main(String[] args) {
        for (int k = 0; k < 10000; k++) {
            System.out.println(k + "=" + toWords(k));
        }
    }

    private static String toWords(int i) {
        StringBuilder buf = new StringBuilder();
        int hundreds = i % 1000;
        int hundredThousands = (i / 1000) % 1000;

        if (hundredThousands == 0 && hundreds == 0) {
            buf.append("Zero");
        } else {
            if (hundredThousands > 0) {
                writeHundreds(buf, hundredThousands);
                buf.append(" thousand");

                if (hundreds > 0) {
                    buf.append(" ");
                }
            }

            if (hundreds > 0) {
                writeHundreds(buf, hundreds);
            }
        }
        return buf.toString();
    }

    private static void writeHundreds(StringBuilder buf, int hundredThousands) {
        int units = hundredThousands % 10;
        int tens = (hundredThousands / 10) % 10;
        int hundreds = (hundredThousands / 100);

        if (hundreds > 0) {
            buf.append(NUMBERS[hundreds]).append(" hundred");
        }

        boolean teen = false;

        if (tens > 0) {
            buf.append(" and ");
            if (tens == 1) {
                buf.append(TEEN_NUMBERS[units]);
                teen = true;
            } else {
                buf.append(TEN_NUMBERS[tens]);

                if (units > 0) {
                    buf.append(" ");
                }
            }
        }

        if (units > 0 && !teen) {
            if (hundreds > 0 && tens == 0) {
                buf.append(" and ");
            }
            buf.append(NUMBERS[units]);
        }
    }
}

只有0 ... 999999,不如其他解决方案好!

答案 2 :(得分:0)

您应该将变量分成十分之几和单位。

下面有些“工作正常的代码”可能需要一些调试......

import java.util.HashMap;

/**
 * Hello world!
 *
 */
public class App
{

    public static void main( String[] args )
    {
        HashMap<Integer, String> numbers = new HashMap<Integer, String>();
            numbers.put(1, "one");
            numbers.put(2, "two");
            numbers.put(3, "three");
            numbers.put(4, "four");
            numbers.put(5, "five");
            numbers.put(6, "six");
            numbers.put(7, "seven");
            numbers.put(8, "eight");
            numbers.put(9, "nine");
            numbers.put(10, "ten");
            numbers.put(11, "eleven");
            numbers.put(12, "twelve");
            numbers.put(13, "thirteen");
            numbers.put(14, "fourteen");
            numbers.put(15, "fifteen");
            numbers.put(16, "sixteen");
            numbers.put(17, "seventeen");
            numbers.put(18, "eighteen");
            numbers.put(19, "nineteen");

        HashMap<Integer, String> tenths = new HashMap<Integer, String>();
            tenths.put(1, "ten");
            tenths.put(2, "twenty");
            tenths.put(3, "thirty");
            tenths.put(4, "forty");
            tenths.put(5, "fifty");
            tenths.put(6, "sixty");
            tenths.put(7, "seventy");
            tenths.put(8, "eighty");
            tenths.put(9, "ninety");


        for (int i=1; i <= 1000000; i++)
        {
            int million = i/1000000;
            int hundred_thousand = i/100000;
            int tenth_thousand = (i%100000)/10000;
            int unit_thousand = ( tenth_thousand == 1 ? (i%20000)/1000 : (i%10000)/1000);
            int hundred = i%1000/100;
            int tenth = (i%100)/10;
            int unit = (tenth == 1 ? i%20: i%10);
            String output = "".concat((million > 0 ? numbers.get(million).concat(" million ") : ""))
                    .concat(hundred_thousand > 0 ? numbers.get(hundred_thousand).concat(" hundred ") : "")
                    .concat(tenth_thousand > 1 ? tenths.get(tenth_thousand).concat(" ") : "")
                    .concat(unit_thousand > 0 ? numbers.get(unit_thousand).concat(" ") : "")
                    .concat( i >= 1000 ? " thousand " : "")
                    .concat(hundred > 0 ? numbers.get(hundred).concat(" hundred ") : "")
                    .concat(tenth > 1 ? tenths.get(tenth).concat(" ") : "")
                    .concat(unit > 0 ? numbers.get(unit) : "");
            System.out.println(output);

        }
    }
}