2016-10-03 120 views
-2

我不知道爲什麼,但是當我試圖編譯下面的代碼我得到錯誤CS1525每)在每個月底同時命令被標記爲錯誤:錯誤CS1525爲什麼會發生?

static void PrintArray(string[] arr) 
{ 
    int i, sum = 0, subb = 0, pow, x; 
    char opper; 
    Console.WriteLine("how many numbers does your calculation have?"); 
    i = Convert.ToInt16(Console.ReadLine()); 
    arr = new string[i]; 
    for (i = 0; i < arr.Length; i++) 
    { 
     Console.WriteLine("enter num {0}" + i); 
     arr[i] = Console.ReadLine(); 
     Console.WriteLine("arr[{0}] = {1}" + i, arr[i]); 
    } 
    Console.WriteLine("what do you want to do?"); 
    opper = Convert.ToChar(Console.ReadLine()); 
    while (opper = +) 
    { 
     for (i = 0; i < arr.Length; i++) 
     { 
      sum = sum + Convert.ToInt16(arr[i]); 
     } 
     Console.WriteLine("your sum is " + sum); 
    } 
    while (opper = -) 
    { 
     for (i = 0; i < arr.Length; i++) 
     { 
      subb = subb + Convert.ToInt16(arr[i]); 
     } 
     Console.WriteLine("your subb is" + subb); 
    } 
    while (opper = *) 
    { 
     pow = Convert.ToInt16(arr[0]); 
     for (i = 1; i < arr.Length; i++) 
     { 
      pow = pow * Convert.ToInt16(arr[i]); 
     } 
     Console.WriteLine("the resolt is " + pow); 
    } 
    while (opper = &) 
    { 
     x = Convert.ToInt16(arr[i]); 
     for (i = 0; i < arr.Length; i++) 
     { 
      x = x/Convert.ToInt16(arr[i]); 
     } 
     Console.WriteLine("your resolt is " + x); 
    } 
     Console.ReadKey(); 
} 

我會很高興,如果有人終於可以解釋給我...

+1

怎麼樣,因爲檢查平等,當你使用''==不是'=' – MethodMan

+1

和你需要的+和 - 符號在引號,因爲它們是字符 - 'while(opper =='+')' – petelids

回答

0

考慮到線(例如)

opper = Convert.ToChar(Console.ReadLine()); 
while (opper = +) 

它看起來像你想的字符輸入比較操作。您將要賦值運算符更改爲比較運營商,和字符的數據進行比較的性格,就像這樣:

opper = Convert.ToChar(Console.ReadLine()); 
while (opper == '+') 
0

user1673882是正確這裏關於編譯錯誤的原因。但是,還應該注意其他幾個重要的錯誤。

至於最初的編譯問題,對於下面的行(和所有類似的行)有兩個問題;

while (opper = +) 

首先,=(單個 「等於」 符號)是分配,比較。您想在此處使用==

其次,+在這種情況下不是字符,它是一種操作。 (事實上​​,編譯器無法準確推斷出哪個運算符可能是)。

即使你得到這個編譯,但它不會工作,因爲你所有的循環都是無限循環。考慮下面這個例子:

char myChar = 'a'; 

     // Infinite loop 
     while (myChar == 'a') 
     { 
      Console.WriteLine("Test"); 
     } 

可能怎麼能拿出來的循環,因爲myChar總是a

其他一些雜項錯誤如下:

subb = subb + Convert.ToInt16(arr[i]); 

這可能與

subb += Convert.ToInt16(arr[i]); 

,甚至可能

subb += (short)arr[i]; 

也可縮短,我假設這不應該是「+」,因爲如果操作是「+」,那麼您正在執行的操作完全相同(即「 +「和」 - 「應該完全一樣)。

x = x/Convert.ToInt16(arr[i]); 

首先,同上清理:

x /= (short)arr[i]; 

其次,你從來沒有在這裏0測試師,所以這可能會拋出異常。

第三,我不知道X是什麼類型的,但「短」絕對不是關閉滿師 - 即:

short a = ... 
short b... 
// May not be another short 
Console.WriteLine(a/b); 

其實,這適用於乘法,減法和在這種情況下也有一定程度的增加,因爲空頭有限。請看下面的代碼:

short overflow = short.MaxValue; 

     // -32768 
     overflow++; 

     // +32767 
     overflow--; 

     // -32768 again 
     overflow++; 

     // -32767 
     overflow++; 

     checked 
     { 
      overflow = short.MaxValue; 

      // Now this results in an OverflowException 
      overflow++; 
     } 

再舉一個例子:

short testArithmetic = 1; 
     // This gives us the result that 1/2 = 0. 
     testArithmetic /= 2; 

     // Set this back to 1 for the next operation 
     testArithmetic = 1; 

     // This is 0.0 too! 
     double testArithmeticFloat = testArithmetic/2; 

     // This gives us the result we'd expect 
     testArithmeticFloat = 1.0/2.0; 

     // This'll compile just fine, but you get a DivideByZeroException when you try to execute it 
     testArithmetic /= 0;