我正在尝试为英语单词程序制作一个简单的数字,我决定使用数组。
它没有在 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;
}
}
}
}