二维数组

发布于 2024-10-09 07:42:34 字数 558 浏览 0 评论 0原文

对于数组中的每个元素,我需要一个唯一的标识符,例如 Seat1、Seat2、Seat 3……一直到数组长度的末尾。

目前我已经完成了以下操作:

int rows = 10, cols = 10;
bool[ , ] seatArray = new bool[rows , cols]; //10 rows, 10 collums

for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++ )
    {
        seatArray[i, j] = false;
    }

    foreach (bool element in seatArray)
    {
        Console.WriteLine("element {0}", element);
    }
}

这只是在控制台中显示“Element False”x 100。

我需要将“Element”替换为 Seat1、Seat2、Seat3... 到数组长度的末尾。

任何帮助将不胜感激!

谢谢你!

For each element in the array I need a unique identifier such as Seat1, Seat2, Seat 3....... all the way to the end of the length of the array.

currently I have done the following:

int rows = 10, cols = 10;
bool[ , ] seatArray = new bool[rows , cols]; //10 rows, 10 collums

for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++ )
    {
        seatArray[i, j] = false;
    }

    foreach (bool element in seatArray)
    {
        Console.WriteLine("element {0}", element);
    }
}

this simply just says "Element False" x 100 in the console.

i need to replace "Element" with Seat1, Seat2, Seat3....to the end of the array length.

any help will be much appreciated!

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉酒的小男人 2024-10-16 07:42:34

创建一个具有 ID 和 Occupied(?) 属性的 Seat 类(或结构,如果更合适的话)。制作一个这种类型的数组。

public class Seat
{
    public string ID { get; set; }
    public bool Occupied { get; set; }
}

int rows = 10, cols = 10;
Seat[,] seats = new Seat[rows,cols];

for (int i = 0; i < rows; ++i )
{
    for (int j = 0; j < cols; ++j)
    {
         seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false };
    }
}

foreach (var seat in seats)
{
    Console.WriteLine( "{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not" );
}

Create a Seat class (or structure, if more appropriate) with ID and Occupied(?) properties. Make an array of this type.

public class Seat
{
    public string ID { get; set; }
    public bool Occupied { get; set; }
}

int rows = 10, cols = 10;
Seat[,] seats = new Seat[rows,cols];

for (int i = 0; i < rows; ++i )
{
    for (int j = 0; j < cols; ++j)
    {
         seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false };
    }
}

foreach (var seat in seats)
{
    Console.WriteLine( "{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not" );
}
梦情居士 2024-10-16 07:42:34
  int count = 1;

for (int i = 0; i < rows; i++)
  for (int j = 0; j < cols; j++ )
  {
    seatArray[i, j] = count;
    count++;
  }

  foreach (bool element in seatArray)
  {
    Console.WriteLine("element {0}", element);
  }

不知道这是什么语言,所以不知道语法,但只需做一些外部计数器来对它们进行编号

,每次将每个计数器设置为 false 时,它​​都会说 false,不要使用 bool,或者编写一个类来保存 true false 和数字信息

  int count = 1;

for (int i = 0; i < rows; i++)
  for (int j = 0; j < cols; j++ )
  {
    seatArray[i, j] = count;
    count++;
  }

  foreach (bool element in seatArray)
  {
    Console.WriteLine("element {0}", element);
  }

no idea what language this is so idk the syntax but just do some external counter to number them

that just says false every time you set every one to false, dont use a bool, or write a class to hold the true false and number info

绾颜 2024-10-16 07:42:34

tvanfosson,我正在努力使您的编码工作正常,iv 将其放入我的主要方法中的一个新类中,请参见下文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Class1
    {
        public class Seat
            {
                public string ID { get; set; }
                public bool Occupied { get; set; }
            }

            int rows = 10, cols = 10;
            Seat[,] seats = new Seat[rows,cols];

            for (int i = 0; i < rows; ++i )
            {
                for (int j = 0; j < cols; ++j)
                {
                     seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false };
                }
            }

            foreach (var seat in seats)
            {
                Console.WriteLine( "{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not" );
            }
    }
}

这是正确的吗,因为我似乎收到了很多语法错误,

谢谢!

tvanfosson, i am struggling to make your coding work, iv put it into a new class off my main method see below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Class1
    {
        public class Seat
            {
                public string ID { get; set; }
                public bool Occupied { get; set; }
            }

            int rows = 10, cols = 10;
            Seat[,] seats = new Seat[rows,cols];

            for (int i = 0; i < rows; ++i )
            {
                for (int j = 0; j < cols; ++j)
                {
                     seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false };
                }
            }

            foreach (var seat in seats)
            {
                Console.WriteLine( "{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not" );
            }
    }
}

is this correct as i seem to be receiving a lot of syntax errors

thank you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文