C# 我可以从Main方法更改类方法中的变量吗?
我正在学习C#,目前正在制作一个零和交叉游戏。 我已经到了一个点,我有一个布尔变量,它决定了玩家的回合,一个输入,玩家想把他们的回合和实际的棋盘放在它自己的类中 这就是我被困的地方。我想用玩家的输入来改变棋盘,但我不知道如何从主方法访问它 下面是我的代码。playerinput是指电路板上的1-9个位置,打印机是电路板对象C# 我可以从Main方法更改类方法中的变量吗?,c#,variables,methods,scope,C#,Variables,Methods,Scope,我正在学习C#,目前正在制作一个零和交叉游戏。 我已经到了一个点,我有一个布尔变量,它决定了玩家的回合,一个输入,玩家想把他们的回合和实际的棋盘放在它自己的类中 这就是我被困的地方。我想用玩家的输入来改变棋盘,但我不知道如何从主方法访问它 下面是我的代码。playerinput是指电路板上的1-9个位置,打印机是电路板对象 class Program { static void Main(string[] args) { --- ---
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));
所以我需要使用playerinput并在PrintBoard方法中更改相应的字母。只要我能改变这些变量,我就可以了
搜索答案时的一个困难是知道如何正确地表达它,因此任何关于此主题的建议或附加阅读都将不胜感激。您可以在
打印板方法中添加一个参数。可能看起来像这样
public void PrintBoard(int playerInput)
{
....
当您从Main
方法调用PrintBoard
方法时,您可以为您的方法提供用户输入并在其中使用它
可能是这样的(假设board
是board
类的一个实例
int playerinput = printer.GetNumber();
board.PrintBoard(playerInput);
您可以查看以了解更多信息。您可以向方法PrintBoard()添加参数,并可以从主方法(如PrintBoard(1,2))传递参数:
然后您可以在打印板方法中指定数字,如:
public void PrintBoard(int a, int b)
{
//Can print the numbers directly.
}
PrintBoard
中的变量不是持久的-它们的持续时间最多与方法的持续时间相同。再次调用PrintBoard
时,任何更改都将丢失
你需要在一个足够长的范围内声明board。例如,Main
方法本身。你已经有了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("---------");
}
}
}
公共课程委员会
{
private char[,]data=new char[3,3];//由“”和“X”或“O”组成的二维数组
//对于无效输入返回false
公共bool HandleInput(int playerInput,char播放器)
{
如果(player!=“X”&&player!=“O”)返回false;//坏玩家
//从1-9输入中获取坐标
变量x=(playerInput-1)%3;
变量y=(playerInput-1)/3;
如果(x<0 | | x>2 | | y<0 | | y>2)返回false;//板外异常
if(data[x,y]!=''返回false;//非空单元格
data[x,y]=player;//设置新的单元格内容
返回true;
}
公开作废印刷品()
{
对于(变量y=0;y<2;y++)
{
对于(变量x=0;x<2;x++)
{
控制台写入(数据[x,y]);
控制台。写(“|”);
}
Console.WriteLine();
Console.WriteLine(--------------”;
}
}
}
问题到底出在哪里?您没有显示如何使用线路板
。很抱歉,在“if(!currentPlayer)”之后,我想添加一些代码,以获取playerinput并更改PrintBoard方法中的相应变量。我不确定如何执行此操作。
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("---------");
}
}
}