0

私はこれのほとんどを完了しましたが、「20 代」を追加する方法について混乱しています。「1 代」と「10 代」はすべて持っていますが、「20 代」がないため、119 で止まります。静的文字列で ThreeDigit は、3 桁の数字を実行するように設定した場所です。

{
class Program
{
    static string[] digitWords =
    { "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine",
        "ten", "eleven", "twelve", "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

    static string[] tenWords =
    { "", "", "twenty", "thirty", "forty",
      "fifty", "sixty", "seventy", "eighty", "ninety" };

    static string[] hundredWords = { "", "One-hundred", "two-hundred", "three-hundred", "four-hundred", "five-hundred", "six-hundred", "seven-hundred", "eight-hundred", "nine-hundred"};

    static string TwoDigit(int num)
    {
        if (num < 0 || num > 1001) return "";
        if (num < 20) return digitWords[num];
        if (num % 10 == 0)
            return tenWords[num / 10];
        else
            return tenWords[num / 10] + "-" + digitWords[num % 10];
    }

    static string ThreeDigit(int num)
    {
        if (num % 100 == 0)
            return hundredWords[num / 100];
        else
            return hundredWords[num / 100] + "-" + digitWords[num % 100];
    }


    static void Main(string[] args)
    {
        for (int i = 0; i <= 19; i++)
            Console.WriteLine("{0}: {1}", i, TwoDigit(i));
        for (int i = 20; i <= 99; i +=7)
            Console.WriteLine("{0}: {1}", i, TwoDigit(i));
        for (int i = 100; i <= 1100; i ++)
            Console.WriteLine("{0}: {1}", i, ThreeDigit(i));
    }
}

}

4

2 に答える 2

2

ここに良い解決策があります: http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words

于 2012-11-15T01:15:23.830 に答える
0

試す:

else
    return hundredWords[num / 100] + " and " + TwoDigit(num);
}
于 2012-11-15T02:04:16.847 に答える