c# 试图使数组映射工作
c# Trying to make an array map work
public static void map_one()
{
const int width = 10;
const int height = 5;
int[,] map = new int[width, height] {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",};
map[playerx, playery] = 1;
for (height = 0; height < 20; height++)
{
for(width = 0; width < 20; width++)
{
Console.Write(map[width, height] + "");
}
Console.WriteLine();
}
}
我在那边看到的数组有问题,它告诉我 "An array initialize of length '10' is expected.Then for the code I wrote out to actually draw the whole thing out, in the loop I get a problem for both height and width "赋值的左侧必须是变量,属性 或索引器
如果您需要更多信息,请询问。
初始化时不需要指定数组维度
像这样使用它
int[,] map = new int[,] {...}
编辑:同样在您的循环中,您将高度和宽度的限制设置为 20,这将导致运行时异常
改为这样做:
int width = map.GetLength(0);
int height = map.GetLength(1);
编辑:
您的最终代码应如下所示
const int width = 10;
const int height = 5;
var map = new int[width, height];
map[playerx, playery] = 1;
for (int h= 0; h< map.GetLength(1); h++)
{
for(int w= 0; w< map.GetLength(0); w++)
{
Console.Write(map[w, h] + "");
}
Console.WriteLine();
}
需要注意的几个问题..
一个:
"0",};
那里有语法错误:)
两个:
您的宽度和高度已声明 "const"。因此,您不能为它们分配任何值 post 声明。
三个:
int[,] map = new int[width, height];
是您所需要的,因为默认情况下该数组中的所有值都将自动初始化为 0。
此外,您不能将 "0"
之类的字符串分配给整数 (int)。
您用一维数组初始化了一个二维数组
应该是这样的
int[,] map = int[2,2] { {0, 0}, {0, 0}};
其次,您在循环中使用的 2 个 index 变量与您在开始时声明的 2 个 const 变量相同,index 应该会改变,但是 const,好吧.. 是常量,不能改变。
只需为您的索引使用另一个变量,如
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
...
哦,不要将字符串值传递给整数数组
public static void map_one()
{
const int width = 10;
const int height = 5;
int[,] map = new int[width, height] {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",};
map[playerx, playery] = 1;
for (height = 0; height < 20; height++)
{
for(width = 0; width < 20; width++)
{
Console.Write(map[width, height] + "");
}
Console.WriteLine();
}
}
我在那边看到的数组有问题,它告诉我 "An array initialize of length '10' is expected.Then for the code I wrote out to actually draw the whole thing out, in the loop I get a problem for both height and width "赋值的左侧必须是变量,属性 或索引器
如果您需要更多信息,请询问。
初始化时不需要指定数组维度
像这样使用它
int[,] map = new int[,] {...}
编辑:同样在您的循环中,您将高度和宽度的限制设置为 20,这将导致运行时异常
改为这样做:
int width = map.GetLength(0);
int height = map.GetLength(1);
编辑:
您的最终代码应如下所示
const int width = 10;
const int height = 5;
var map = new int[width, height];
map[playerx, playery] = 1;
for (int h= 0; h< map.GetLength(1); h++)
{
for(int w= 0; w< map.GetLength(0); w++)
{
Console.Write(map[w, h] + "");
}
Console.WriteLine();
}
需要注意的几个问题..
一个:
"0",};
那里有语法错误:)
两个:
您的宽度和高度已声明 "const"。因此,您不能为它们分配任何值 post 声明。
三个:
int[,] map = new int[width, height];
是您所需要的,因为默认情况下该数组中的所有值都将自动初始化为 0。
此外,您不能将 "0"
之类的字符串分配给整数 (int)。
您用一维数组初始化了一个二维数组 应该是这样的
int[,] map = int[2,2] { {0, 0}, {0, 0}};
其次,您在循环中使用的 2 个 index 变量与您在开始时声明的 2 个 const 变量相同,index 应该会改变,但是 const,好吧.. 是常量,不能改变。
只需为您的索引使用另一个变量,如
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
...
哦,不要将字符串值传递给整数数组