8

即使这个问题之前已经发布并回答过。我需要有关我的代码的帮助。任务是将一个数字转换为从 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;
    }
}
4

7 回答 7

5
while (number >= 0)

您的代码似乎正在创建一个无限循环。举个例子:

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

这不会退出您的循环,因此永远不会到达

txtConvertedNumber.Text = ConvertedNumber;

使用支票转换您的号码,例如:

if (number == 0)
{
    ConvertedNumber = "Zero";
    number = -1;
}
于 2013-08-04T20:06:11.980 回答
4

这可能有用

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;
    }
于 2013-08-05T05:15:14.560 回答
1

您应该在 while 循环之前检查数字是否为零

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

然后修改while如下

while(number > 0)
于 2013-08-04T20:18:09.497 回答
1

这可能有助于十进制数

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

String Word=NumberToWords(convert.toInt64(number[0])) + " AND " +     NumberToWords(convert.toInt64(number[1]));
于 2014-05-07T10:38:23.333 回答
0

这也可以使您的代码更易于调试/遵循。

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

将此方法nameSpacebtnConvertToText_Click.

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

一般来说,我建议不要在您的代码中添加幻数,例如 +1s 或 -1s。它可以使调试更加繁琐。如果您必须这样做才能使某些东西起作用,请尝试找出原因并尽快重写它。绝对是在使用其他方法之前。

于 2013-08-04T20:35:39.020 回答
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];
                  }


              }
          }
于 2013-08-04T20:26:30.323 回答
0
    class Program
    {
        public static string DecimalToText(string decimalPart)
        {
            string[] digits = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
            string result = "";
            foreach (char c in decimalPart)
            {
                int i = (int)c - 48;
                if (i < 0 || i > 9) return ""; // invalid number, don't return anything
                result += " " + digits[i];
            }
            return result;
        }

        public static string NumberToText(int number, bool useAnd)
        {
            if (number == 0) return "Zero";



            if (number == -2147483648) return "Minus Two Hundred " + "and " + "Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred " + "and " + "Forty Eight";

            int[] num = new int[4];
            int first = 0;
            int u, h, t;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (number < 0)
            {
                sb.Append("Minus ");
                number = -number;
            }
            string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
            string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
            string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
            string[] words3 = { "Thousand ", "Lakh ", "Crore " };
            num[0] = number % 1000; // units
            num[1] = number / 1000;
            num[2] = number / 100000;
            num[1] = num[1] - 100 * num[2]; // thousands
            num[3] = number / 10000000; // crores
            num[2] = num[2] - 100 * num[3]; // lakhs
            for (int i = 3; i > 0; i--)
            {
                if (num[i] != 0)
                {
                    first = i;
                    break;
                }
            }

            for (int i = first; i >= 0; i--)
            {
                if (num[i] == 0) continue;

                u = num[i] % 10; // ones 
                t = num[i] / 10;
                h = num[i] / 100; // hundreds
                t = t - 10 * h; // tens

                if (h > 0) sb.Append(words0[h] + "Hundred ");
                if (u > 0 || t > 0)
                {
                    if (h > 0 || i < first) sb.Append("and ");

                    if (t == 0)
                        sb.Append(words0[u]);
                    else if (t == 1)
                        sb.Append(words1[u]);
                    else
                        sb.Append(words2[t - 2] + words0[u]);
                }
                if (i != 0) sb.Append(words3[i - 1]);
            }

            string temp = sb.ToString().TrimEnd();


            return temp;
        }

        static void Main(string[] args)
        {
            double d = 1234.56;
            int i = (int)d;
            string decimalPart = d.ToString().Split('.')[1];
            string text = NumberToText(i, true) + " Point" + DecimalToText(decimalPart);
            Console.WriteLine(text);
            Console.ReadKey();
        }
    }
于 2020-03-28T10:17:51.000 回答