你好,我剛剛完成了一個網格的代碼。這些說明可讓您右移,左移,上移或下移並輸入特定值以移動。但是現在的問題是,輸入哪個值是唯一成爲真實的位置。我試圖讓全部變成真實。例如fi我們右移3,它應該是3點= true而不是最後一個。任何人都可以幫助我嗎?布爾二維數組C#使多個值爲真
// Make a bool 2d array and save already drilled values into the array
bool[,] drill = new bool[401, 200];
drill[200, 1] = true;
drill[200, 2] = true;
drill[200, 3] = true;
drill[201, 3] = true;
drill[202, 3] = true;
drill[203, 3] = true;
drill[203, 4] = true;
drill[203, 5] = true;
drill[204, 5] = true;
drill[205, 5] = true;
drill[205, 4] = true;
drill[205, 3] = true;
drill[206, 3] = true;
drill[207, 3] = true;
drill[207, 4] = true;
drill[207, 5] = true;
drill[207, 6] = true;
drill[207, 7] = true;
drill[206, 7] = true;
drill[205, 7] = true;
drill[204, 7] = true;
drill[203, 7] = true;
drill[202, 7] = true;
drill[201, 7] = true;
drill[200, 7] = true;
drill[199, 7] = true;
drill[199, 6] = true;
drill[199, 5] = true;
// Set some values
bool okay = true;
int column = -1;
int row = -5;
int check_column = 199;
int check_row = 5;
// Run a while loop
do
{
// Ask the user to input a direction
Console.WriteLine("Which direction would you like to go?");
string direction = Console.ReadLine();
// Ask the user to input a movement
Console.WriteLine("What value would you like to move by?");
string distance = Console.ReadLine();
int new_distance = Convert.ToInt32(distance);
// Use if statements
if (direction == "l" || direction == "L")
{
column = column - new_distance;
check_column = check_column - new_distance;
}
else if (direction == "u" || direction == "U")
{
row = row + new_distance;
check_row = check_row - new_distance;
}
else if (direction == "r" || direction == "R")
{
column = column + new_distance;
check_column = check_column + new_distance;
}
else if (direction == "d" || direction == "D")
{
row = row - new_distance;
check_row = check_row + new_distance;
}
else if (direction == "q" || direction == "Q" || new_distance == 0)
{
Console.WriteLine("Program will now end.");
okay = false;
break;
}
while (new_distance > 0)
{
if (drill[check_column, check_row] == true && check_row >= 0 && check_row <=200 && check_column >=0 && check_column <=400 && drill[check_column, check_row] != true)
{
Console.WriteLine("{0},{1} safe", column, row);
break;
}
else
{
Console.WriteLine("{0},{1} danger", column, row);
Console.WriteLine("Program will now end.");
okay = false;
break;
}
}
} while (okay);
if (okay == false)
{
Console.WriteLine("Thanks for using the program!");
}
Console.ReadLine();
幾件事情1)'如果(好吧==假)'將永遠是真實的,因此沒有必要,因爲你只能從做突破,而循環後設置好=假2)在你的while循環中,你有競爭檢查'鑽[check_column,check_row] == true && .. &&鑽[check_column,check_row]!= true' 3)你永遠不會遞減 'new_distance'在你的while循環中4)你能否澄清你的意思_3 spots = true_。我沒有看到你在哪裏設置任何東西,除了在你的初始化 –
好吧,例如,如果我要輸入「r」,然後「2」,那麼它應該使網格上的這兩個點是真實的,但現在,它的唯一將EXACT值2設置爲原來的值爲true – SaltyLegend