【问题标题】:Can I change variables in class methods from Main method?我可以从 Main 方法更改类方法中的变量吗?
【发布时间】:2017-07-18 11:31:51
【问题描述】:

我正在学习 C#,目前正在制作 Naughts and Crosses 游戏。 我已经达到了一个点,我有一个布尔变量来决定玩家轮到,玩家想要将轮到的输入和实际的棋盘放在它自己的类中。

这就是我卡住的地方。我想获取玩家输入并使用它来更改板,但无法弄清楚如何从 Main 方法访问它。

下面是我的代码。 playerinput 是指板上的 1-9 位置,而打印机是 Board 对象。

class Program
{
    static void Main(string[] args)
    {
        ---
        ---
        ---

        int playerinput = printer.GetNumber();

        if (!currentPlayer)
        {
        // I want to add code here that takes playerinput 
        // and changes the corresponding place on the board.
        }

这是实际的板子。

    public class Board
{ ----
public void PrintBoard()
    {
        var a = 1;
        var b = 2;
        var c = 3;
        var d = 4;
        var e = 5;
        var f = 6;
        var g = 7;
        var h = 8;
        var i = 9;

        System.Console.Writeline(string.Format(@" {0} | {1} | {2}
-----------
 {3} | {4} | {5} 
-----------
 {6} | {7} | {8} ", a, b, c, d, e, f, g, h, i));

所以我需要在PrintBoard方法中取playerinput并更改对应的字母。只要我能改变这些变量,我应该没问题。

寻找答案时的一个困难是知道如何正确地用词,因此任何关于这个主题的建议或额外阅读都将不胜感激。

【问题讨论】:

  • 问题到底出在哪里?你没有展示你是如何使用Board的。
  • 对不起,在 'if (!currentPlayer)' 之后,我想添加一些代码来接受 playerinput 并更改 PrintBoard 方法中的相应变量。我不知道该怎么做。

标签: c# variables methods scope


【解决方案1】:

PrintBoard 中的变量不是持久性的 - 它们最多与方法一样长。当您再次拨打PrintBoard 时,所有更改都将丢失。

您需要在一个持续时间足够长的范围内声明该板。例如,Main 方法本身。您已经有一个 Board 对象的实例,所以这将是放置它的明显位置 - 只需将这些变量声明为字段,而不是方法中的局部变量。

Board 对象上的一个方法可能正在处理玩家输入;这只是一种将玩家输入作为参数的方法,并相应地更新棋盘。

作为另一个建议,考虑阅读数组 - 它们是管理结构化数据的便捷方式,例如您在此处的网格。你可以这样做:

public class Board
{
  private char[,] data = new char[3, 3]; // A 2D array of ' ', 'X' or 'O'

  // Returns false for invalid input
  public bool HandleInput(int playerInput, char player)
  {
    if (player != 'X' && player != 'O') return false; // Bad player

    // Get coördinates from the 1-9 input
    var x = (playerInput - 1) % 3;
    var y = (playerInput - 1) / 3;

    if (x < 0 || x > 2 || y < 0 || y > 2) return false; // Out-of-board-exception

    if (data[x, y] != ' ') return false; // Non-empty cell

    data[x, y] = player; // Set the new cell contents

    return true;
  }

  public void Print()
  {
    for (var y = 0; y < 2; y++)
    {
      for (var x = 0; x < 2; x++)
      {
        Console.Write(data[x, y]);
        Console.Write(" | ");
      }

      Console.WriteLine();
      Console.WriteLine("---------");
    }
  }
}

【讨论】:

    【解决方案2】:

    您可以在PrintBoard 方法中添加一个参数。可能是这样的

    public void PrintBoard(int playerInput)
    {
        ....
    

    当您从Main 方法调用PrintBoard 方法时,您可以将用户输入提供给您的方法并在其中使用它。

    可能看起来像这样(假设 board 是您的 Board 类的一个实例。

    int playerinput = printer.GetNumber();
    board.PrintBoard(playerInput);
    

    您可以查看Method Parameters 了解更多信息。

    【讨论】:

      【解决方案3】:

      您可以向 PrintBoard() 方法添加参数,并可以从 Main 方法中传递参数,例如 PrintBoard(1,2):

      public void PrintBoard(int a, int b)
      

      然后您可以在 PrintBoard 方法中分配数字,例如:

           public void PrintBoard(int a, int b)
          {
          //Can print the numbers directly.
          }
      

      【讨论】:

        猜你喜欢
        • 2014-01-29
        • 2017-03-27
        • 2015-07-20
        • 1970-01-01
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        • 2011-06-19
        相关资源
        最近更新 更多