2016-10-03 82 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; 
相关问题