0

我正在学习 Java 初学者课程并研究方法。我的任务是将输入的 1-999 之间的数字打印为单词。除了数百个中的任何“青少年”数字(110、114、212、919 等)之外,所有这些数字都可以正常工作。普通青少年工作正常,但我对任何 3 位数数字识别 - 青少年的方法(因为他们会有第二个1) 的数字不是。

4

3 回答 3

2

我运行了你的程序,它似乎-teens在 3 位数(例如 110,114 等)中运行良好

-teens但是,它不适用于 14、17等两位数。

你的代码

if((numInput>=10)&&(numInput<=19)){//will initiate the teens subroutine
teens(numInput);}

应改为

if((numInput>=10)&&(numInput<=19)){//will initiate the teens subroutine
teens(onesDigit);}

teens() 子例程将个位数字作为参数,而不是整数。

此外,不需要hundredsTeens变量。你可以直接通过onesDigit

于 2013-06-09T14:03:31.413 回答
1

我认为您忘记了代码中的某些内容。

您已经确定了两种情况:十位数字是否为 1。有三种情况。

1- <1

2- ==1

3->1

然后你使用双 tensDigit = (numInput % 100) / 10; 但这不是数字!114 返回 1.4,您应该将数字声明为整数。

先试试这个(数字为双...):

    if (tensDigit < 1){
            ones(onesDigit); // only display the last digit
        }
    else if(1==Integer.parseInt(Double.toString(tensDigit).substring(0, 1))){
            teens(hundredsTeens);
        }
        else if (tensDigit > 1){
            tens(tensDigit);
            System.out.print(" ");
            ones(onesDigit);
        }
    }

你会看到你的错误,然后试着用实数来简化你的代码可读性。

于 2013-06-09T14:54:47.523 回答
0

这是使用某些 Java 8 以文字形式打印从 0 到 99999 的数字的答案。运行将数字作为命令行参数传递的程序。

public static final Map<Integer, String> DICTIONARY = Collections.unmodifiableMap(Stream
    .of(new SimpleEntry<>(0, "zero"), new SimpleEntry<>(1, "one"), new SimpleEntry<>(2, "two"),
            new SimpleEntry<>(3, "three"), new SimpleEntry<>(4, "four"), new SimpleEntry<>(5, "five"),
            new SimpleEntry<>(6, "six"), new SimpleEntry<>(7, "seven"), new SimpleEntry<>(8, "eight"),
            new SimpleEntry<>(9, "nine"), new SimpleEntry<>(10, "ten"), new SimpleEntry<>(11, "eleven"),
            new SimpleEntry<>(12, "tweleve"), new SimpleEntry<>(13, "thirteen"), new SimpleEntry<>(14, "fourteen"),
            new SimpleEntry<>(15, "fifteen"), new SimpleEntry<>(16, "sixteen"), new SimpleEntry<>(17, "seventeen"),
            new SimpleEntry<>(18, "eighteen"), new SimpleEntry<>(19, "nineteen"), new SimpleEntry<>(20, "twenty"),
            new SimpleEntry<>(30, "thirty"), new SimpleEntry<>(40, "forty"), new SimpleEntry<>(50, "fifty"),
            new SimpleEntry<>(60, "sixty"), new SimpleEntry<>(70, "seventy"), new SimpleEntry<>(80, "eighty"),
            new SimpleEntry<>(90, "ninety"), new SimpleEntry<>(100, "hundred"), new SimpleEntry<>(1000, "thousand"))
    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));





public static void main(String args[]) {
    try {
        if (args.length == 0 || args.length > 1) {
            throw new RuntimeException(
                    "Please run the program with only one number in the range of 0 to 99999");
        }
        Integer givenNumber = Integer.parseInt(args[0]);
        NumbersEnglishDictionary.DICTIONARY.entrySet()
            .stream()
            .filter(e -> e.getKey().equals(givenNumber))
            .findAny()
            .ifPresent(System.out::print);
        if (givenNumber < 100) {
            System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 10) * 10)
                    + " " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
            return;
        }
        if (givenNumber > 100 && givenNumber < 1000) {
            System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber / 100)
                    + " hundred " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10) + " "
                    + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
            return;
        }
        if (givenNumber > 1000 && givenNumber < 10000) {
            System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber / 1000)
                    + " thousand " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 100) % 10) + " hundred "
                    + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10) + " "
                    + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
            return;
        }
        if (givenNumber > 10000 && givenNumber < 100000) {
            System.out.print(
                    givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 1000) / 10) * 10)
                            + " " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 1000) % 10)
                            + " thousand " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 100) % 10)
                            + " hundred " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10)
                            + " " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
            return;
        }
    } catch (NumberFormatException e) {
        throw new RuntimeException("Please run the program with only one number in the range of 0 to 99999");
    }
}
于 2018-03-16T20:33:57.697 回答