数字到使用数组的单词

时间:2012-11-23 18:08:13

标签: c# arrays

我正在尝试为英语单词程序编写一个简单的数字,我决定使用数组。但是每当我输入一个大于99的数字时,我在第三个if子句中都会出错。我需要更改什么来解决这个问题?这是我的代码;

    class Program
    {
        static void Main(string[] args)
        {
         string[] uptonineteen = {"Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
        "Sixteen","Seventeen","Eighteen","Nineteen"};
        string[] ten = {"","","Twenty","Thirty","Forty","Fifty",
        "Sixty","Seventy","Eighty","Ninety",};
        Console.WriteLine(" ---------------");
        int i = int.Parse(Console.ReadLine());
            if (i < 20)
            {
                Console.WriteLine(uptonineteen[i]);
            }
            if (i < 100)
            {
                Console.WriteLine(ten[i / 10] + ((i % 10 > 0) ? "" + uptonineteen[i%10] : ""));
            }
            if (i <= 999)
            {
                object lenthree = ten[i / 100] + "hundred"+" " + ((i % 100 > 0) ? "and" +" "+  uptonineteen[i % 1000] : "");
                Console.WriteLine(lenthree);
            } 
            Console.ReadKey();
        }
    }
}

5 个答案:

答案 0 :(得分:1)

我已经看过太多次了......这个处理过,比如写支票写作,你可能需要花费数百万,最后包括美分......

public class NumToWords
{
    public NumToWords()
    {}

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

    // empty string for zero-place-holder for tens grouping
    string[] Tens = { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

    // leave blank for "hundreds" because you would NOT do 123 as One hundred twenty-three hundred.
    string[] Block3Section = { "", "thousand", "million", "billion" };


    public string ToEnglish(int num)
    {
        int Block3Seq = 0;
        int curSegment;
        int tmp;
        string FinalWords = "";
        string curWord = "";

        while (num > 0)
        {
            curSegment = num % 1000;    // get only 3 digits worth (right) of the number
            num = (num - curSegment) / 1000;  // subtract this portion from the value

            if (curSegment > 0)
            {
                // always add the closing word as we HAVE something to build out
                curWord = "";

                // how many "hundreds" of the current segment
                tmp = (int)curSegment / 100;
                if (tmp > 0)
                    curWord += " " + OneToNineteen[tmp] + " hundred";

                // what is remainder of this 100 based segment
                tmp = curSegment % 100;

                if (tmp < 20)
                    curWord += " " + OneToNineteen[tmp];
                else
                {
                    curWord += " " + Tens[(int)tmp / 10]
                                + '-' + OneToNineteen[tmp % 10];
                }

                // always add the closing word as we HAVE something to build out
                curWord += " " + Block3Section[Block3Seq];

                // add the section above to the overall words
                FinalWords = curWord + FinalWords;
            }

            // to allow the next closing word for segment such as thousand, million, billion, etc
            Block3Seq++;
        }

        return FinalWords;
    }

}

答案 1 :(得分:1)

试试这个......

class Program
{
    static void Main(string[] args)
    {
        string[] uptonineteen = {"Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
        "Sixteen","Seventeen","Eighteen","Nineteen"};
        string[] ten = {"", "","Twenty","Thirty","Forty","Fifty",
        "Sixty","Seventy","Eighty","Ninety",};
        Console.WriteLine(" ---------------");
        int i = int.Parse(Console.ReadLine());
        if (i < 20)
        {
            Console.WriteLine(uptonineteen[i]);
        }
        else if (i < 100)
        {
            Console.WriteLine(((((i % 100) / 10) > 1) ? ten[((i % 100) / 10)] + ((i % 10) > 0 ? " " + uptonineteen[(i % 10)] : "") : " and " + uptonineteen[i % 100]));
        }
        else if (i <= 999)
        {
            object lenthree = uptonineteen[(i % 1000) / 100] + " hundred " + ((((i % 100) / 10) > 1) ? ten[((i % 100) / 10)] + ((i % 10) > 0 ? " " + uptonineteen[(i % 10)] : "") : " and " + uptonineteen[i % 100]);
            Console.WriteLine(lenthree);
        } 
        Console.ReadLine();
    }
}

答案 2 :(得分:0)

string lenthree = ten[i / 100] + "hundred" + " " + 
    ((i % 100 > 0) ? "and" + " " + uptonineteen[i % 100] : "");

最后1000个应为100.但由于uptonineteen未满,您仍然无法正确输入完整数字。

答案 3 :(得分:0)

处理案例number > x后,您应该继续使用相同的余下的number = number % xnumber %= x。另外,你应该真正区分获取英文文本和在控制台上进行测试。 Console.WriteLinesConsole.ReadKeys不应出现在逻辑中。使用此常规结构使用StringBuilder构建英语字符串:

public static string ToEnglish(int number)
{
    var sb = new StringBuilder();
    if (number >= 100) {
        ...
        number %= 100;
    }
    if (sb.Length > 0 && number != 0) {
        sb.Append("and ");
    }
    if (number >= 20) {
        ...
        number %= 10;
    }
    if (sb.Length == 0 || number != 0) {
        ...
    }
    return sb.ToString();
}

注意:自从我做作业以来,我没有发布完整的解决方案。


控制台测试看起来像这样

while (true) {
    Console.Write("Please enter a number or `q` to quit: ");
    string s = Console.ReadLine();
    if (s == "q") {
        break;
    }
    int i = int.Parse(s);
    Console.WriteLine(ToEnglish(i));
}

答案 4 :(得分:0)

string[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
                            "eleven", "twelve", "thirtheen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen","nineteen", "twenty" };
string[] tens = { "Ten", "Twenty", "Thirthy", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

        Console.WriteLine("Enter a number");
        int i = int.Parse(Console.ReadLine());
        if (i < 20)
        {
            Console.WriteLine(ones[i]);
        }
        if (i > 30  & i < 100)
        {
            Console.WriteLine(tens[((i%100)/10)-1]+ " " + ones[(i % 10)]);
        }
        if (i > 100 & i < 1000)
        {
            if (((i % 100) % 10) == 0)
            {
                Console.WriteLine(ones[(i / 100)] + " hundred " + tens[((i % 100) / 10) - 1]);
            }
            else
            {
                Console.WriteLine(ones[(i / 100)] + " hundred " + tens[((i % 100) / 10) - 1] + " and " + ones[((i % 100) % 10)]);
            }
        }
        Console.ReadLine();