C# - 更改事件中的变量?

时间:2018-01-22 23:04:07

标签: c# events integer

在阅读之前,我想指出我是一名在C#工作的高中学生,并且从互联网上学习了大部分知识。

我遇到问题,当我在int_player1_religion = 1;中设置pictureBox_buddhism_click整数时,当我点击pictureBox_taoism_click时,它不会保留值(或重置它)。我需要点击一个,然后点击另一个,但是当第二个需要识别它是被点击的第二个按钮时,它们都被计为第一次点击。

我认为问题在于设置整数,但如果是这种情况,我不太确定在哪里放置它们。什么是错的? (将有更多的按钮,而不仅仅是当前的两个(Buhhdism和道教),但只有两次点击来声明player1和player2的角色。)

代码:

public partial class Form2 : Form
{
    int int_player1_religion = 0;  // <=====
    int int_player2_religion = 0;  // <=====
    string string_player1_religion;
    string string_player2_religion;
    public Form2()
    {
        InitializeComponent(); //1 = buddhism, 12 = celtic polytheism 
        button1.Visible = false;
    }

    private void pictureBox_buddhism_Click(object sender, EventArgs e)
    {   
        if (int_player1_religion == 0) {
            int int_player1_religion = 1; // <=====
            string string_player1_religion = "buddhism";
            pictureBox2.BackgroundImage = Properties.Resources.religion_buddhism;
            label2.Text = "player 2 choose your religion";
            label3.Text = "";
            pictureBox_buddhism.Visible = false;
            button2.Text = "buddhism";
        }

        if (int_player1_religion != 0)
        {
            int int_player2_religion = 1;  // <=====
            string string_player2_religion = "buddhism";
            pictureBox4.BackgroundImage = Properties.Resources.religion_buddhism;
            label2.Text = "start the game";
            label3.Text = "";
            pictureBox_buddhism.Visible = false;
            button3.Text = "buddhism";
            button1.Visible = true;
        }
    }

    private void pictureBox_taoism_Click(object sender, EventArgs e)
    {
        if (int_player1_religion == 0)
        {
            int int_player1_religion = 2;  // <=====
            string string_player1_religion = "taoism";
            pictureBox2.BackgroundImage = Properties.Resources.religion_taoism;
            label2.Text = "player 2 choose your religion";
            label4.Text = "";
            pictureBox_taoism.Visible = false;
            button2.Text = "taoism";
        }
        if (int_player1_religion != 0)
        {
            int int_player2_religion = 2;  // <=====
            string string_player2_religion = "taoism";
            pictureBox4.BackgroundImage = Properties.Resources.religion_taoism;
            label2.Text = "start the game";
            label4.Text = "";
            pictureBox_taoism.Visible = false;
            button3.Text = "taoism";
            button1.Visible = true;
        }
    }
}

}

&lt; =====评论是我认为事情出错的地方。

1 个答案:

答案 0 :(得分:0)

首先,我想鼓励你继续学习!在高中学习C#编程并不容易,因为大多数人在大学或在工作中都这样做。

无论如何,问题出在你的第二次声明中

int int_player1_religion = 1; // <=====

在你的第二次宣言中

int int_player2_religion = 1;

你看,这个语句创建了一个全新的int_player1_religion变量,它只在你声明它的if块中有效 - 即使它与你在前几行中声明的int_player1_religion变量共享同一个名字,它们实际上是在记忆中完全分开。

为了修改/访问你想要的int_player1_religion变量,取出int_player1_religion = 1前面的“int”部分;和int_player1_religion = 2;在您最初声明它的第3行和第4行之后的任何地方。

希望这有帮助!

public partial class Form2 : Form
{
    int int_player1_religion = 0;  // <=====
    int int_player2_religion = 0;  // <=====
    string string_player1_religion;
    string string_player2_religion;
    public Form2()
    {
        InitializeComponent(); //1 = buddhism, 12 = celtic polytheism 
        button1.Visible = false;
    }

    private void pictureBox_buddhism_Click(object sender, EventArgs e)
    {   
        if (int_player1_religion == 0) {
            int_player1_religion = 1; // <===== take out the int
            string string_player1_religion = "buddhism";
            pictureBox2.BackgroundImage = Properties.Resources.religion_buddhism;
            label2.Text = "player 2 choose your religion";
            label3.Text = "";
            pictureBox_buddhism.Visible = false;
            button2.Text = "buddhism";
        }

        if (int_player1_religion != 0)
        {
            int_player2_religion = 1;  // <===== take out the int
            string string_player2_religion = "buddhism";
            pictureBox4.BackgroundImage = Properties.Resources.religion_buddhism;
            label2.Text = "start the game";
            label3.Text = "";
            pictureBox_buddhism.Visible = false;
            button3.Text = "buddhism";
            button1.Visible = true;
        }
    }

    private void pictureBox_taoism_Click(object sender, EventArgs e)
    {
        if (int_player1_religion == 0)
        {
            int_player1_religion = 2;  // <===== take out the int
            string string_player1_religion = "taoism";
            pictureBox2.BackgroundImage = Properties.Resources.religion_taoism;
            label2.Text = "player 2 choose your religion";
            label4.Text = "";
            pictureBox_taoism.Visible = false;
            button2.Text = "taoism";
        }
        if (int_player1_religion != 0)
        {
            int_player2_religion = 2;  // <===== take out the int
            string string_player2_religion = "taoism";
            pictureBox4.BackgroundImage = Properties.Resources.religion_taoism;
            label2.Text = "start the game";
            label4.Text = "";
            pictureBox_taoism.Visible = false;
            button3.Text = "taoism";
            button1.Visible = true;
        }
    }
}