【问题标题】:error CS1525 why does it happen?错误 CS1525 为什么会发生?
【发布时间】:2017-02-11 20:52:43
【问题描述】:

我不知道为什么,但是当我尝试编译下一个代码时,我收到了错误 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();
}

如果有人终于可以向我解释这一点,我会很高兴...

【问题讨论】:

  • 怎么样,因为在检查相等性时你使用== 而不是=
  • 你需要引号中的 + 和 - 符号,因为它们是字符 - while (opper == '+')

标签: c# compiler-errors syntax-error


【解决方案1】:

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”绝对是 not 封闭于除法之上 - 即:

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;

【讨论】:

    【解决方案2】:

    给定行(例如)

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多