【问题标题】:How do I output values from these two subroutines?如何从这两个子例程中输出值?
【发布时间】:2016-12-13 09:57:00
【问题描述】:

我正在尝试调用 XCoordinate 和 YCoordinate 以及网格以在主中显示它,我该怎么做?

public static void DisplayBoard(char[,] Board)
    {
        string[,] grid = new string[3, 3] {{"   ","   ","   "},
                                           {"   ","   ","   "},
                                           {"   ","   ","   "}};

        string board = System.IO.File.ReadAllText("H:\\BoardGame.txt");
        Console.WriteLine(grid);           
    }


    public static void GetMoveCoordinates(ref int XCoordinate, ref int YCoordinate)
    {
        int CommaLocation;
        bool GameHasBeenWon = false;
        string CoordinatesInput;
        string XChar, YChar;
        while (GameHasBeenWon == false)
        {
            try
            {
                Console.Write("Enter your coordinates: (x,y) ");
                CoordinatesInput = Console.ReadLine();
                CommaLocation = CoordinatesInput.IndexOf(",".ToString());
                XChar = CoordinatesInput.Substring(CommaLocation - 1);
                YChar = CoordinatesInput.Substring(CommaLocation + 1);
                XCoordinate = int.Parse(XChar);
                YCoordinate = int.Parse(YChar);
            }
            catch
            {
                Console.WriteLine("Invalid Input- Please Try Again");
            }
        }
    }
}

【问题讨论】:

标签: c# subroutine


【解决方案1】:

你这样称呼它:

int x = 0;
int y = 0;
GetMoveCoordinates(ref x, ref y)
Console.WriteLine("Move was {0},{1}",x,y)

【讨论】:

    【解决方案2】:

    嗯……

    string[,] grid = new string[3, 3] {{"   ","   ","   "},
                                           {"   ","   ","   "},
                                           {"   ","   ","   "}};
    
    public void AddMove(int x, int y, string p)
    {
        grid(x,y) = $" {p} ";
        Console.SetCursorPosition(0,0)
        for(int i=0;i<3;i++) 
        {
            for(int j=0;j<3;j++) 
            {
                Console.Write(grid(i,j));
            }
            Console.Write("\n");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2019-08-02
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多