2013-08-04 71 views
6

即使此問題已發佈並在之前回答。我想幫助我的代碼。任務是將一個數字轉換爲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; 
    } 
} 
+2

[在單詞C#轉換的數字]的可能重複(http://stackoverflow.com/questions/2729752 /轉換成數字的單詞-c-sharp) –

+2

你是用調試器完成代​​碼的嗎? – GreatBigBore

+1

我認爲這是http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words你想要什麼。 – RahulD

回答

5
while (number >= 0) 

好像你的代碼創建一個無限循環。舉個例子:

if (number == 0) 
{ 
    ConvertedNumber = "Zero"; 
} 

這不退出你的循環,因此永遠不會到達

txtConvertedNumber.Text = ConvertedNumber; 

用單向轉換您的號碼,如:

if (number == 0) 
{ 
    ConvertedNumber = "Zero"; 
    number = -1; 
} 
+0

我想每個數字都會讓答案爲零! –

+1

如果您包含正確的檢查並確保如果數字在其他地方變爲零,它將自動變爲'-1'而不是'0' – bas

1

您應該檢查如果在while循環之前數字爲零

if (number == 0) 
     ConvertedNumber= "Zero" ; 

然後修改而如下

while(number > 0) 
0

好像它停留在循環..我到過這裏的青少年數量的部分,似乎 您忘了遞減數小於零或零

   else if (number >= 10 && number < 100) 
       { 
       if(number > 10) 
       { 
        if (number > 10 && number < 20) 
        { 
         for (int x = 11; x < 20; x++) 
         { 
          if (number == x) 
          { 
           ConvertedNumber += teens[x - 11]; 

          } 
         } 
         ***number -= 100;*** 
        } 

        else 
        { 
         number -= 10; 
         Count++; 
         ConvertedNumber += tens[Count - 1]; 
        } 


       } 
      } 
0

這也會起作用,並使您的代碼更易於調試/遵循。

while (NumberExists(ConvertedNumber, number)) 
{ 
    ... 
} 

放置該方法在同一nameSpace作爲btnConvertToText_Click

private static bool NumberExists(string convertedNumber, int number) 
    { 
     if (convertedNumber == " " && number != null) 
     { 
      return true; 
     } 
     else 
      return false; 
    } 

一般來說,我建議不要在您的代碼中添加魔術數字,比如+ 1或-1。它可以使調試更加繁瑣。如果你必須這樣做才能找到工作,試着找出原因並儘快重寫。在使用其他方法之前肯定是這樣。

2

這可能是有用的

public static string NumberToWords(int number) 
    { 
     if (number == 0) 
      return "Zero"; 

     if (number < 0) 
      return "Minus " + NumberToWords(Math.Abs(number)); 

     string words = ""; 

     if ((number/1000000) > 0) 
     { 
      words += NumberToWords(number/1000000) + " Million "; 
      number %= 1000000; 
     } 

     if ((number/1000) > 0) 
     { 
      words += NumberToWords(number/1000) + " Thousand "; 
      number %= 1000; 
     } 

     if ((number/100) > 0) 
     { 
      words += NumberToWords(number/100) + " Hundred "; 
      number %= 100; 
     } 

     if (number > 0) 
     { 
      if (words != "") 
       words += "and "; 

      var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; 
      var tensMap = new[] { "zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; 

      if (number < 20) 
       words += unitsMap[number]; 
      else 
      { 
       words += tensMap[number/10]; 
       if ((number % 10) > 0) 
        words += "-" + unitsMap[number % 10]; 
      } 
     } 

     return words; 
    } 
1

這可有助於爲十進制數

string[] number = Number1.ToString().Split('.'); 

String Word=NumberToWords(convert.toInt64(number[0])) + " AND " +  NumberToWords(convert.toInt64(number[1])); 
+0

這是怎麼回事這個問題? –