即使这个问题之前已经发布并回答过。我需要有关我的代码的帮助。任务是将一个数字转换为从 0 到 1000 万的单词。我试图用我的代码使用 GUI 来做到这一点,问题是它根本不返回答案。任何人都可以帮助我确定代码上的问题可能是什么。
下面的代码:
private void btnConvertToText_Click(object sender, EventArgs e)
{
string ConvertedNumber = " ";
int number = Convert.ToInt32(txtNumber.Text);
int Count = 0;
string [] ones = {" One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine "};
string [] teens= {" Eleven ", " Twelve ", " Thirteen ", " Fourtte n ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen "};
string [] tens = {" Ten ", " Twenty ", " Thirty ", " Fourty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninenty "};
string [] moreThenTens = {" hundred ", " thousand ", " million "};
while (number >= 0)
{
if (number == 0)
{
ConvertedNumber = "Zero";
}
else if (number < 10)
{
for (int Counter = 1; Counter < 10; Counter++)
{
ConvertedNumber += ones[Counter-1];
}
}
else if (number >= 10 && number < 100)
{
while (number > 10)
{
if (number > 10 && number < 20)
{
for (int x = 11; x < 20; x++)
{
if (number == x)
{
ConvertedNumber = teens[x - 11];
}
}
}
else
{
number -= 10;
Count++;
}
ConvertedNumber += tens[Count - 1];
}
}
else if (number >= 100 && number < 1000)
{
while (number > 100)
{
number -= 100;
Count++;
}
ConvertedNumber += ones[Count - 1] + moreThenTens[0];
}
else if (number >= 1000 && number < 10000)
{
while (number > 1000)
{
number -= 1000;
Count++;
}
ConvertedNumber += ones[Count - 1] + moreThenTens[1];
}
else if (number >= 10000 && number < 100000)
{
while (number > 10000)
{
if (number < 20000)
{
number -= 10000;
Count++;
}
else if (number >= 20000)
{
number -= 10000;
Count++;
}
}
if (Count >= 1)
{
if (number < 1000)
{
ConvertedNumber += tens[0] + moreThenTens[1];
}
else if (number >= 1000)
{
ConvertedNumber += teens[Count - 1] + moreThenTens[1];
}
}
else if (Count > 1)
{
ConvertedNumber += tens[Count - 1] + moreThenTens[1];
}
}
else if (number >= 100000 && number < 1000000)
{
while (number > 100000)
{
number -= 100000;
Count++;
}
ConvertedNumber += ones[Count - 1] + moreThenTens[0] + moreThenTens[1];
}
else if (number >= 1000000 && number < 10000000)
{
while (number > 1000000)
{
number -= 1000000;
Count++;
}
ConvertedNumber += ones[Count - 1] + moreThenTens[2];
}
else if (number == 10000000)
{
while (number > 10000000)
{
number -= 10000000;
Count++;
}
ConvertedNumber += tens[0] + moreThenTens[2];
}
}
txtConvertedNumber.Text = ConvertedNumber;
}
}