首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误CS0560:错误数组声明器;错误CS0270:数组大小不能在变量声明中指定

错误CS0560:错误数组声明器;错误CS0270:数组大小不能在变量声明中指定
EN

Stack Overflow用户
提问于 2022-09-09 01:04:56
回答 1查看 78关注 0票数 0

当我试图运行一些代码时,我会得到以下错误:

代码语言:javascript
复制
/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]

这是我试图运行的代码块。

代码语言:javascript
复制
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#应该是有效的。

是什么导致了这些错误,我如何纠正它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-09 01:19:36

b1b2在单个语句中声明和赋值的方式导致了此错误。

您可以更改它,以便将值直接赋值给每个变量,如下所示:

代码语言:javascript
复制
bool b1 = bools[0], b2 = bools[1];

或者像这样把它们解压成一个元组:

代码语言:javascript
复制
var (b1, b2) = (bools[0], bools[1]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73656350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档