2010-08-31 24 views
0
英語詞彙

任何人可以幫我調試這個...執行bolow代碼時..轉換整數到使用C#.NET

Error 1 Cannot implicitly convert type 'string' to 'long' 
Error 2 The name 'inputNum' does not exist in the current contex 

    protected void Button1_Click(object sender, EventArgs e) { 
     var englishTranslation = IntegerToWords(inputNum); 
    } 

    public string IntegerToWords(long inputNum) { 
     int dig1, dig2, dig3, level = 0, lasttwo, threeDigits; 

     var retval = ""; 
     var x = ""; 
     string[] ones = { 
          "zero", 
          "one", 
          "two", 
          "three", 
          "four", 
          "five", 
          "six", 
          "seven", 
          "eight", 
          "nine", 
          "ten", 
          "eleven", 
          "twelve", 
          "thirteen", 
          "fourteen", 
          "fifteen", 
          "sixteen", 
          "seventeen", 
          "eighteen", 
          "nineteen" 
         }; 
     string[] tens = 
      { 
       "zero", 
       "ten", 
       "twenty", 
       "thirty", 
       "forty", 
       "fifty", 
       "sixty", 
       "seventy", 
       "eighty", 
       "ninety" 
      }; 
     string[] thou = 
      { 
       "", 
       "thousand", 
       "million", 
       "billion", 
       "trillion", 
       "quadrillion", 
       "quintillion" 
      }; 

     var isNegative = false; 
     if (inputNum < 0) { 
      isNegative = true; 
      inputNum *= -1; 
     } 

     if (inputNum == 0) 
      return ("zero"); 

     var s = inputNum.ToString(); 

     while (s.Length > 0) { 
      // Get the three rightmost characters 
      x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3); 

      // Separate the three digits 
      threeDigits = int.Parse(x); 
      lasttwo = threeDigits%100; 
      dig1 = threeDigits/100; 
      dig2 = lasttwo/10; 
      dig3 = (threeDigits%10); 

      // append a "thousand" where appropriate 
      if (level > 0 && dig1 + dig2 + dig3 > 0) { 
       retval = thou[level] + " " + retval; 
       retval = retval.Trim(); 
      } 

      // check that the last two digits is not a zero 
      if (lasttwo > 0) { 
       if (lasttwo < 20) // if less than 20, use "ones" only 
        retval = ones[lasttwo] + " " + retval; 
       else // otherwise, use both "tens" and "ones" array 
        retval = tens[dig2] + " " + ones[dig3] + " " + retval; 
      } 

      // if a hundreds part is there, translate it 
      if (dig1 > 0) 
       retval = ones[dig1] + " hundred " + retval; 

      s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : ""; 
      level++; 
     } 

     while (retval.IndexOf(" ") > 0) 
      retval = retval.Replace(" ", " "); 

     retval = retval.Trim(); 

     if (isNegative) 
      retval = "negative " + retval; 

     return (retval); 
    } 

我有以下錯誤,我是新來的編程...

+0

在面試時看起來像一個測試 – Arseny 2010-08-31 07:38:34

+0

提醒我在大學第一年。非常懷舊。 – 2010-08-31 16:57:27

回答

0

錯誤不是邏輯錯誤。
1.檢查發生錯誤的行號。使用long.Parse(str)將字符串轉換爲long。
2.變量inputNum未在Button1_Click函數中聲明。聲明它,爲它指定值(從一個文本框也許?),它必須被轉換。

+0

謝謝..你的回答對我有用:) – Priya 2010-08-31 09:20:48

+0

如果你不是在等待更好的答案,請你選擇我的答案作爲正確的答案(左邊的剔號),我需要一點名譽,我想要添加賞金問題:) – lalli 2010-08-31 09:59:53