c# - 对象 C# 的二维数组
问题描述
我知道,这个问题似乎被问了很多,但我仍然无法使用人们提供的解决方案来解决它。因此,在尝试使用二维对象数组时,我也遇到了异常“System.NullReferenceException:'对象引用未设置为对象的实例。'”。所以这是不工作的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_multi_fonctionnelle_basique
{
partial class Program
{
public class SudokuSolver
{
//Initialisation code
public SudokuSolver()
{
GridValue[,] SudokuGrid = new GridValue[9, 9];
SudokuDisplay(SudokuGrid);
Console.ReadKey();
}
//Store values for every slot
class GridValue
{
public bool CanBe1 { get; set; } = false;
public bool CanBe2 { get; set; } = false;
public bool CanBe3 { get; set; } = false;
public bool CanBe4 { get; set; } = false;
public bool CanBe5 { get; set; } = false;
public bool CanBe6 { get; set; } = false;
public bool CanBe7 { get; set; } = false;
public bool CanBe8 { get; set; } = false;
public bool CanBe9 { get; set; } = false;
public bool AlreadySolved { get; set; } = false;
public int Value { get; set; } = 0;
}
//Display the grid
void SudokuDisplay(GridValue[,] Sudoku)
{
Sudoku[1, 1].Value = 1;
Sudoku[1, 2].Value = 2;
Console.WriteLine(Sudoku[0, 0].Value + Sudoku[1,0].Value);
}
//To Do
//Ask the values for every slot
//Verify if the slot can contain a number
//Choose the most appropriated number for the slot
//End the program after user pressing an key
}
}
}
因此,如您所见,我想使用 9x9 大小的“GridValue”类的 2D 数组,以便每个“Grid slot”都有自己的变量,以便以后解决数独问题。但是程序看起来不理解我的数组包含“GridValue”对象所以他似乎不理解每个数组值都包含多个变量......所以我想要的结果是我可以为其中一个对象定义变量数组没有异常。
解决方案
您创建了一个空数组,因此当您尝试访问一个元素时,会引发异常。初始化数组的元素,你应该没问题。
public class SudokuSolver
{
//Initialisation code
public SudokuSolver()
{
GridValue[,] SudokuGrid = new GridValue[9, 9];
//###################
for (int r=0; r < 9; r++)
{
for (int c = 0; c < 9; c++)
{
SudokuGrid[r, c] = new GridValue();
}
}
//###################
SudokuDisplay(SudokuGrid);
Console.ReadKey();
}
//Store values for every slot
class GridValue
{
public bool CanBe1 { get; set; } = false;
public bool CanBe2 { get; set; } = false;
public bool CanBe3 { get; set; } = false;
public bool CanBe4 { get; set; } = false;
public bool CanBe5 { get; set; } = false;
public bool CanBe6 { get; set; } = false;
public bool CanBe7 { get; set; } = false;
public bool CanBe8 { get; set; } = false;
public bool CanBe9 { get; set; } = false;
public bool AlreadySolved { get; set; } = false;
public int Value { get; set; } = 0;
}
//Display the grid
void SudokuDisplay(GridValue[,] Sudoku)
{
Sudoku[1, 1].Value = 1;
Sudoku[1, 2].Value = 2;
Console.WriteLine(Sudoku[0, 0].Value + Sudoku[1, 0].Value);
}
//To Do
//Ask the values for every slot
//Verify if the slot can contain a number
//Choose the most appropriated number for the slot
//End the program after user pressing an key
}
推荐阅读
- python - 从python字典制作二维密度图
- vba - 更新链接时自动回复消息框
- ios - UIButton 不会覆盖 ButtonType 属性
- iis - 无法侦听给定端口 .net core 2.0 IIS
- angular - 如何检查多个订阅是否达到 onComplete()?
- php - PHP:CHECK 记录可用性 SQL
- c++ - 如何组合多个连续图像来模拟逼真的运动模糊?
- jenkins - Jenkins Pipeline 定义和设置变量
- android-studio - 无法运行安卓工作室
- jquery - 为什么音频对象在不播放音频时为 toISOString() 提供错误“无效时间值”?