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

C#-在事件中更改变量?,c#,events,integer,C#,Events,Integer,在阅读之前,我想指出我是一名在C#工作的高中生,我的大部分学习都是通过互联网完成的 我遇到了一个问题,当我设置int\u player1\u religation=1

在阅读之前,我想指出我是一名在C#工作的高中生,我的大部分学习都是通过互联网完成的

我遇到了一个问题,当我设置
int\u player1\u religation=1pictureBox\u\u单击时,
pictureBox\u\u单击时,它不会保留值(或重置值)。我需要单击一个按钮,然后单击另一个按钮,但是当第二个按钮需要识别它是被单击的第二个按钮时,这两个按钮都被视为第一次单击

我相信问题在于设置整数,但如果是这样的话,我不太确定把它们放在哪里。有什么问题吗?(除了当前的两个按钮(Buhhdism和Taao),还有更多的按钮,但只需点击两次即可声明player1和player2的角色。)

代码:

公共部分类表单2:表单
{

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

不管怎么说,这个问题在你的第二次声明中


int-int-player1\u-religation=1;//您正在创建一个与类作用域同名的局部作用域变量。当作用域发生变化时,局部作用域变量消失。在if语句末尾。如果要设置类变量,只需
int-player2\u-religation=2
,而不是
int-int-player2\u-religation=2;
不要使用
if(x)
后跟
if(!x)
——使用
if(x){…}else{…}
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;
        }
    }
}
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;
        }
    }
}