我正在學習如何編程,在主要用於web之前我已經使用了其他語言,但c#至少對我來說有點難以理解,而msdn站點太複雜或者我不知道不知道如何正確使用它。努力構建解決數獨謎題程序,我遇到問題時,我嘗試的方法來顯示二維數組,獲得2個錯誤:在方法中顯示二維數組
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[,] board = new int[9, 9] {{7, 2, 5, 8, 0, 4, 9, 1, 3},
{0, 0, 0, 0, 3, 0, 0, 0, 4},
{0, 0, 0, 0, 0, 1, 8, 2, 6},
{0, 0, 9, 0, 0, 0, 0, 0, 7},
{0, 1, 0, 6, 2, 8, 0, 4, 0},
{2, 0, 0, 0, 0, 0, 5, 0, 0},
{3, 6, 8, 4, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 7, 0, 0, 0, 8},
{0, 0, 0, 0, 0, 2, 6, 9, 5}};
DisplayBoard(board);
List<int> missing = new List<int>();
List<int> present = new List<int>();
int temp = 0;
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 9; j++)
{
//Console.Write(board[i, j]);
if (board[i, j] == 0)
{
missing.Add(j);
}
else
{
present.Add(board[i, j]);
}
if (present.Count == 8)
{
for (int x = 0; x < present.Count; x++)
{
if (!present.Contains(x))
{
temp = x;
}
}
board[i, missing[0]] = temp;
}
}
//missing.ForEach(Console.WriteLine);
}
DisplayBoard(board);
}
static void DisplayBoard(int[,])
{
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Write(board[i, j]);
}
}
}
}
有什麼錯誤? – Guy
循環'for(int i = 0; i <1; i ++)'的目的是什麼?現在完全沒用。我敢打賭,你需要檢查我是否低於9。 –
@SergeyBerezovskiy它應該是我<9,但同時測試即時只嘗試與第一行。 – fakerismyguru