不显示具有特定循环迭代索引的字符串数组

时间:2013-04-18 09:37:42

标签: c# arrays

我正在尝试为英语单词程序编写一个简单的数字,我决定使用数组。

它没有在tens2数组中显示循环迭代值,因为我在字符串数组中声明了它。如果我想用特定的循环迭代索引显示字符串数组tens2,该怎么办。

我需要更改什么来解决这个问题?这是我的代码;

static void Main(string[] args)
{

    Console.Write("Enter Number =");
    string n = Console.ReadLine();

    char[] num = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    string[] unit = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    string[] tens = new string[] { "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    string[] tens2 = new string[] { "","twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };


    if (n.Length == 4)
    {

        for (int a = 0; a < num.Length; a++)
        {
            if (n[0].Equals(num[a]))
            {
                Console.Write("{0}" + " " + "Thousand", unit[a]);
                break;
            }
        }
        for (int a = 0; a < num.Length; a++)
        {
            if (n[1].Equals(num[a]))
            {
                Console.Write(" {0}" + " " + "Hundred", unit[a]);
                break;
            }
        }
        for (int a = 0; a < num.Length; a++)

        {
            if (n[2]=='1' && n[3].Equals(num[a]))
            {
                Console.Write(" {0}",tens[a+1]);
                break;
            }
            else if (n[2].Equals('1') && n[3].Equals('0'))
            {
                Console.Write(" Ten");
                break;
            }
            else if (n[2]!='1' && n[3].Equals(num[a]))
            {
                Console.Write(" {0}"  , tens2[a]);
                break;
            }

        }
            for (int a = 0; a < num.Length; a++)
        {

            if ((n[3].Equals(num[a]) && (n[2]!='1')))
            {
                Console.Write(" {0}" , unit[a]);
                break;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

仅限四位数字:

string n = "5415";

int[] digits = n.Select(i => Int32.Parse(i.ToString())).ToArray();

string[] unit = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string[] tens = new string[] { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tens2 = new string[] { "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

StringBuilder sb = new StringBuilder();

sb.Append(unit[digits[0]-1]);
sb.Append(" thousand, ");

if (digits[1] > 0)
{
    sb.Append(unit[digits[1] - 1]);
    sb.Append(" hundred and ");
}

if (digits[2] > 1)
{
    sb.Append(tens2[digits[2] - 1]);
    sb.Append(" ");
    sb.Append(unit[digits[3] - 1]);
}
else if (digits[2] == 1)
{
    sb.Append(tens[digits[3]]);
}
else
{
    sb.Append(unit[digits[3] - 1]);
}

答案 1 :(得分:0)

我修改了@ paul的代码,使其更加健壮:

    static void Main(string[] args)
    {
        Console.Write("Enter Number =");
        string n = Console.ReadLine();
        var sb = new StringBuilder();

        if (n.Length == 4)
        {
            if (int.Parse(n) <= 0)
            {
                sb.Append("Supply a positive integer number greater than zero.");
            }
            else
            {
                int[] digits = n.Select(i => Int32.Parse(i.ToString())).ToArray();

                string[] unit = new string[]
                    {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
                string[] tens = new string[]
                    {
                        "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
                        "eighteen",
                        "nineteen"
                    };
                string[] tens2 = new string[]
                    {"", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

                if (digits[0] > 0)
                {
                    sb.Append(unit[digits[0] - 1]);
                    sb.Append(" thousand, ");
                }

                if (digits[1] > 0)
                {
                    sb.Append(unit[digits[1] - 1]);
                    sb.Append(" hundred and ");
                }

                if (digits[2] > 1)
                {
                    sb.Append(tens2[digits[2] - 1]);
                    sb.Append(" ");
                    sb.Append(unit[digits[3] - 1]);
                }
                else if (digits[2] == 1)
                {
                    sb.Append(tens[digits[3]]);
                }
                else
                {
                    sb.Append(unit[digits[3] - 1]);
                }
            }

            Console.WriteLine(sb.ToString());
        }
        else
        {
            Console.WriteLine("Only four digit number is supported in trial version of this program !!!");
        }

        Console.ReadKey();
    } 

现在它优雅地支持任何值。 121212,1,0000,0001,0011,0101,1001等