错误CS1525为什么会发生?

时间:2016-10-03 20:08:16

标签: c# compiler-errors syntax-error

我不知道为什么,但是当我尝试编译下一个代码时,我收到错误CS1525,并且每个while命令结束时的每个)都被标记为错误:

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();
}

如果有人能够最终向我解释,我会很高兴...

2 个答案:

答案 0 :(得分:0)

给出行(例如)

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

您似乎正在尝试将字符输入与运营商进行比较。您将要将赋值运算符更改为比较运算符,并将该字符与另一个字符进行比较,如下所示:

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

答案 1 :(得分:0)

user1673882在这里关于编译错误的原因是正确的。但是,您还应该注意其他一些重要的错误。

对于原始编译问题,您对以下行(以及所有类似的行)有两个问题;

while (opper = +)

首先,=(单个&#34;等于&#34;符号)是分配,不是比较。您想在此处使用==

其次,+在这种情况下不是一个字符,它是一个操作。 (事实上​​,编译器无法准确推断它可能是哪个运算符。)

即使你要编译它,它也不会起作用,因为你的所有循环都是无限循环。考虑这个例子:

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];

另外,我假设这不应该是&#34; +&#34;因为如果操作是&#34; +&#34;那么你正在进行的操作完全相同(即&#34; +&#34;和&#34; - &#34;的结果应完全相同。)

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

首先,同样的清理工作:

x /= (short)arr[i];

其次,你从不在这里用0测试除法,所以这可能会引发异常。

第三,我不确定 x 是什么类型,但是&#34;短&#34;肯定关闭分裂 - 即:

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;