Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言中为2D数组分配空格/空值#_C#_Arrays_2d - Fatal编程技术网

C# 如何在C语言中为2D数组分配空格/空值#

C# 如何在C语言中为2D数组分配空格/空值#,c#,arrays,2d,C#,Arrays,2d,我正试图打印一个带有空值的Int 2D数组。这基本上类似于在字符串数组中分配一个空格 int[,] arr = new int[10, 10]; for (int temp = 0; temp < arr.GetLength(0); temp++) { for (int temp2 = 0; temp2 < arr.GetLength(1); temp2++) { arr[temp, temp2] = xxxxx ; } } for (int xIndex =

我正试图打印一个带有空值的Int 2D数组。这基本上类似于在字符串数组中分配一个空格

int[,] arr = new int[10, 10];
for (int temp = 0; temp < arr.GetLength(0); temp++)
{
 for (int temp2 = 0; temp2 < arr.GetLength(1); temp2++)
  {
    arr[temp, temp2] = xxxxx ;
   }
}

for (int xIndex = 0; xIndex < arr.GetLength(0); xIndex++)
{
  for (int yLoop = 0; yLoop < arr.GetLength(1); yLoop++)
   {
    Console.Write(arr[xIndex, yLoop]);
    }
    Console.WriteLine();
}
int[,]arr=新的int[10,10];
对于(int-temp=0;temp
到目前为止,我尝试分配null值,但我得到“无法将null转换为int”,因为它是不可为null的值类型。
如果我不指定任何值,它只是简单地输出0,我希望它在控制台中不显示任何内容。

您不能将NaN值指定给int。int的每个可能值都是一个数字,如果您希望不显示任何内容而不是0,您必须以某种方式解决这个问题。

您不能将NaN值指定给int。每个可能的值都是一个数字int的值是一个数字,如果希望不显示任何内容而不是0,则必须以某种方式解决此问题。

如果2D数组包含空值,只需检查该值是否为空,如果为空,则不打印任何内容。这里的问题是数组被声明为
int
数组,因此它不能包含空值。
一种可能的解决方法是,如果实际数组值都为正值,则将其赋值为负值,而不是“null”。这样,您就可以检查该值

int[,] arr = new int[10, 10];
for (int temp = 0; temp < arr.GetLength(0); temp++) {
  for (int temp2 = 0; temp2 < arr.GetLength(1); temp2++) {
    arr[temp, temp2] = xxxxx; // set to -1 if the value is unknown / undefined etc.
  }
}

for (int xIndex = 0; xIndex < arr.GetLength(0); xIndex++) {
  for (int yLoop = 0; yLoop < arr.GetLength(1); yLoop++) {
    if (arr[xIndex, yLoop] >= 0) {
      Console.Write(arr[xIndex, yLoop]);
    }
    else {
      Console.Write(" ");
    }
  }
  Console.WriteLine();
}
int[,]arr=新的int[10,10];
对于(int-temp=0;temp=0){
Write(arr[xIndex,yLoop]);
}
否则{
控制台。写(“”);
}
}
Console.WriteLine();
}

如果2D数组包含空值,只需检查该值是否为空,如果为空,则不打印任何内容。这里的问题是数组被声明为
int
数组,因此它不能包含空值。
一种可能的解决方法是,如果实际数组值都为正值,则将其赋值为负值,而不是“null”。这样,您就可以检查该值

int[,] arr = new int[10, 10];
for (int temp = 0; temp < arr.GetLength(0); temp++) {
  for (int temp2 = 0; temp2 < arr.GetLength(1); temp2++) {
    arr[temp, temp2] = xxxxx; // set to -1 if the value is unknown / undefined etc.
  }
}

for (int xIndex = 0; xIndex < arr.GetLength(0); xIndex++) {
  for (int yLoop = 0; yLoop < arr.GetLength(1); yLoop++) {
    if (arr[xIndex, yLoop] >= 0) {
      Console.Write(arr[xIndex, yLoop]);
    }
    else {
      Console.Write(" ");
    }
  }
  Console.WriteLine();
}
int[,]arr=新的int[10,10];
对于(int-temp=0;temp=0){
Write(arr[xIndex,yLoop]);
}
否则{
控制台。写(“”);
}
}
Console.WriteLine();
}

您可以尝试将
int
设置为可为null的类型,以便为其分配null。这应该是真的,或者所有其他值类型,如
double
bool
。只需将
int[,]
更改为
int?[,]

您可以尝试将
int
设置为可空类型,以便为其分配空值。这应该是真的,或者所有其他值类型也应该是像
double
bool
。只需将
int[,]
更改为
int?[,]
如果您只想打印到控制台,只需使用If条件打印一个空格即可

int[,]arr=新的int[10,10]; //您不需要赋值。默认情况下为0

for (int xIndex = 0; xIndex < arr.GetLength(0); xIndex++)
{
  for (int yLoop = 0; yLoop < arr.GetLength(1); yLoop++)
   {
    if(arr[xIndex, yLoop]==0)
    {
       Console.Write(" ");
    }
   }
    Console.WriteLine();
}
for(int-xIndex=0;xIndex
如果您只想打印到控制台,只需使用If条件打印一个空格即可

int[,]arr=新的int[10,10]; //您不需要赋值。默认情况下为0

for (int xIndex = 0; xIndex < arr.GetLength(0); xIndex++)
{
  for (int yLoop = 0; yLoop < arr.GetLength(1); yLoop++)
   {
    if(arr[xIndex, yLoop]==0)
    {
       Console.Write(" ");
    }
   }
    Console.WriteLine();
}
for(int-xIndex=0;xIndex
将int定义为可空类型将解决您的问题,只需将数组声明为:


int?[,]arr=new int?[10,10];

将int定义为可空类型将解决您的问题,只需将数组声明为:

int?[,]arr=newint?[10,10];