当我试图运行一些代码时,我会得到以下错误:
/home/runner/Relational-Operators/Operators/Logical.cs(23,42): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. [/home/runner/Relational-Operators/main.csproj]
/home/runner/Relational-Operators/Operators/Logical.cs(23,43): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) [/home/runner/Relational-Operators/main.csproj]这是我试图运行的代码块。
using System;
namespace Operators
{
class Logical
{
public static void Run()
{
Console.WriteLine("Enter two binary digits > ");
int[] ints = new int[2]{Convert.ToInt32(Console.Read()), Convert.ToInt32(Console.Read())};
bool[] bools = new bool[2];
for (int i = 0; i < 2; i++)
{
switch (ints[i])
{
case 0: bools[i] = false; break;
case 1: bools[i] = true; break;
default:
throw new InvalidOperationException("Integer value is not valid");
}
}
bool b1, b2 = bools[0], bools[1];
if (b1 && b2)
{
Console.WriteLine("true");
}
if (b1 || b2)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
if (!(b1 && b2))
{
Console.WriteLine("true");
}
}
}
}我不知所措。我找不到解决错误的办法。从我迄今所读到的所有内容来看,这段代码对于C#应该是有效的。
是什么导致了这些错误,我如何纠正它们?
发布于 2022-09-09 01:19:36
b1和b2在单个语句中声明和赋值的方式导致了此错误。
您可以更改它,以便将值直接赋值给每个变量,如下所示:
bool b1 = bools[0], b2 = bools[1];或者像这样把它们解压成一个元组:
var (b1, b2) = (bools[0], bools[1]);https://stackoverflow.com/questions/73656350
复制相似问题