我有一个控制台应用程序,它显示一个数组,并根据用户的输入将数字移到“ 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";
     }

关于c# - 更新阵列的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17241359/

10-13 09:00