我知道,这个问题似乎很多,但仍然无法使用人们给出的解决方案来解决。因此,在尝试使用2D对象数组时,我也遇到了异常“ System.NullReferenceException:'对象引用未设置为对象的实例。'”。因此,这是无效代码:
df1.merge(df2, on='A',how='outer',indicator =True).rename(columns={'_merge':'merge'}).merge(df3, on='A',how='outer',indicator =True)
因此,就像您看到的那样,我想使用大小为9x9的“ GridValue”类的2D数组,以便每个“ Grid slot”都有自己的变量以供以后解决数独问题。但是程序看起来好像不了解我的Array包含“ GridValue”对象,因此他似乎不了解每个Array值都包含多个变量...所以我想要的结果是我可以为其中的一个对象定义变量数组而没有异常。
答案 0 :(得分:0)
我希望这个例子可以帮助您理解OOP。欢迎来到社区!
using System;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello World!");
SudokuSolver sudokuSolver = new SudokuSolver(9, 9);
sudokuSolver.DisplayGrid();
Console.ReadKey();
}
}
public class SudokuSolver
{
//Store every values needed for every "slot"
public 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;
}
public GridValue[,] SudokuGrid { get; }
//Code d'initialisation
public SudokuSolver(int x, int y)
{
// only initialize in the constructor, no calls
SudokuGrid = new GridValue[x, y]; // the array exists now in mem, but each entry is pointing to null
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
SudokuGrid[i, j] = new GridValue();
}
}
}
//Display the grid => then name it that way!
public void DisplayGrid()
{
SudokuGrid[1, 1].Value = 1;
SudokuGrid[1, 2].Value = 2;
Console.WriteLine(SudokuGrid[0, 0].Value + SudokuGrid[1, 0].Value);
}
}
}
答案 1 :(得分:0)
您创建了一个空数组,因此当您尝试访问元素时,将引发异常。 初始化数组的元素,就可以了。
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
}