我正在使用c#为Windows窗体应用程序创建一个简单的骰子游戏。
现在,我在将播放器切换为1或2时遇到了问题。
首先,当我点击开始按钮时,它会显示1或2的播放器编号,然后文本框将被禁用。 - >这部分没问题......可以和玩家1或2一起玩
但是,当我想结束从玩家1成为玩家2的游戏时。 它应该更改数字并禁用播放器1的文本框。
问题是当我点击END按钮时,它显示玩家编号变为0.
private void btnPlay_Click(object sender, EventArgs e)
{
Random RndRandom;
RndRandom = new Random();
int player = RndRandom.Next(1, 3);
tbxOutputPlayer.Text = player.ToString();
if (player == 1)
{
tbxPlayer2.Enabled = false;
tbxPlayer1.Enabled = true;
//tbxOutputPlayer.Text = "Player 1 start the game";
}
else if (player == 2)
{
tbxPlayer1.Enabled = false;
tbxPlayer2.Enabled = true;
//tbxOutputPlayer.Text = "Player 2 start the game";
}
}
private void btnEnd_Click(object sender, EventArgs e)
{
if (player == 1)
{
player = 2;
tbxOutputPlayer.Text = player.ToString();
}else if (player == 2)
{
player = 1;
tbxOutputPlayer.Text = player.ToString();
}
//if(player == 1)
//{
// tbxOutputPlayer.Text = "player 1 ends the game";
// tbxPlayer2.Enabled = true;
// tbxPlayer1.Enabled = false;
//}
//else if (player == 2)
//{
// tbxOutputPlayer.Text = "player 2 ends the game";
// tbxPlayer1.Enabled = true;
// tbxPlayer2.Enabled = false;
//}
}
答案 0 :(得分:0)
请尝试以下代码,我希望它能够正常运作。
private void btnEnd_Click(object sender, EventArgs e)
{
if (Convert.ToString(tbxOutputPlayer.Text)== "1")
{
tbxOutputPlayer.Text = "2";
}else if (Convert.ToString(tbxOutputPlayer.Text)== "2")
{
tbxOutputPlayer.Text = "1";
}
}