即使此問題已發佈並在之前回答。我想幫助我的代碼。任務是將一個數字轉換爲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;
}
}
[在單詞C#轉換的數字]的可能重複(http://stackoverflow.com/questions/2729752 /轉換成數字的單詞-c-sharp) –
你是用調試器完成代碼的嗎? – GreatBigBore
我認爲這是http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words你想要什麼。 – RahulD