【问题标题】:Switch another player (after END the game, it should be switch another player)换其他玩家(结束游戏后,应该换其他玩家)
【发布时间】:2018-05-07 20:05:06
【问题描述】:

我正在使用 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,它不会改变播放器。现在,我已经解决了这个问题,因为我两次声明了播放器。一个在播放按钮内部,另一个在外部。
  • 您是否在程序的其他位置设置了player(为零)?
  • 您可能对播放器有两种定义。您的班级中的一个,您不触摸或设置为 0 以及您在 btnPlay_Click 中本地定义的一个
  • 当你点击结束时,请检查你在变量 Player 中得到了什么。

标签: c# winforms


【解决方案1】:

请尝试下面的代码,我希望它会工作。

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";
        }

    }

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2015-10-03
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多