我有一个控制台应用程序,它显示一个数组,并根据用户的输入将数字移动到“0”的左侧、右侧、顶部或底部
这是我的代码:
int[,] myNos = new int[3, 3] { { 3, 0, 7 }, { 9, 4, 8 }, { 2, 1, 5 } };
public void WriteData()
{
Console.SetCursorPosition(0, 4);
for (int col = 0; col < 3; col++)
{
for (int row = 0; row < 3; row++)
{
Console.Write("{0}\t", myNos[col, row]);
}
Console.WriteLine();
}
}
public void Level1()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.Write("Score : {0}", score);
Console.SetCursorPosition(30, 0);
Console.Write("Moves : {0}", moves);
WriteData();
string move = "a";
while (move != "E")
{
Console.SetCursorPosition(0, 8);
Console.Write("\nEnter your Move [L,R,T,B,E] : ");
move = Console.ReadLine();
move = move.ToUpper();
while (move == "L")
{
if (myNos[0, 0] == 0)
{
WriteData();
}
else if (myNos[0, 1] == 0)
{
myNos[0, 1] = myNos[0, 0];
myNos[0, 0] = 0;
WriteData();
}
else if (myNos[0, 2] == 0)
{
myNos[0, 2] = myNos[0, 1];
myNos[0, 1] = 0;
WriteData();
}
else
{
WriteData();
}
}
}
}
一切都如我所愿,但唯一的问题是在更新并重新显示数组后,光标不会返回到它将读取输入的位置。我试过使用:
Console.SetCursorPosition(0, 8);
和:
Console.ReadKey();
但似乎都不起作用。
请您参考如下方法:
我想到了一些结果证明是答案的东西,我将移动变量声明为全局变量,然后修改了我的 WriteData() 方法和
public void WriteData()
{
Console.SetCursorPosition(0, 4);
for (int col = 0; col < 3; col++)
{
for (int row = 0; row < 3; row++)
{
Console.Write("{0}\t", myNos[col, row]);
}
Console.WriteLine();
}
//This was the modification
menu = "a";
}
