c# 使用 Blocks 外部的字符串对其进行编辑

c# Using strings on the outside of Blocks to edit it

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string aaxe = null;
    string apick = null;
    string asho = null;
    string acan = null;
    string aknife = null;
    string akey = null;
    string atre = null;

当我点击斧头时(aaxe)。将 atre 的字符串编辑为 "y" 而不是 null。 但是当我尝试这样做时,它没有在 aaxe 块内找到 atre 。 那我该如何解决呢?

public void Axe_Click(object sender, EventArgs e)
{
    if (aaxe == null)
    {
        richTextBox1.AppendText("You Don't Have An Axe\r\n");
    }

    if (aaxe == "y")
    {
        richTextBox1.AppendText("You Used your Axe\r\n");
    }
}

这是“atre”代码

    public void Treasure_Click(object sender, EventArgs e)
    {
        if (atre == null)
        {
            richTextBox1.AppendText("You Haven't Found the Treasure Yet!\r\n");
        string aaxe = "y"

它找不到 aaxe

        }
        if (atre == "y")
        {
            richTextBox1.AppendText("You Found The Treasure!!!\r\n");
            richTextBox1.AppendText("The End Of The Adventure!");
        }
    }

您正在重新声明变量。那是你的问题。声明变量时,可以设置类型:

string aaxe = null;
string atre = null;

当你调用一个变量时,你只需要通过变量名来调用它:

aaxe = "y";

像这样:

public partial class Form1 : Form
{
    // Set the variables here
    string aaxe = null;
    string atre = null;

    public Form1()
    {
        InitializeComponent();
    }

    public void Treasure_Click(object sender, EventArgs e)
    {
        if (atre == null)
        {
            richTextBox1.AppendText("You Haven't Found the Treasure Yet!\r\n");
            // When you call a variable, you don't need to add the 'string' type. 
            aaxe = "y"
        }
        if (atre == "y")
        {
            richTextBox1.AppendText("You Found The Treasure!!!\r\n");
            richTextBox1.AppendText("The End Of The Adventure!");
        }
    }

    public void Axe_Click(object sender, EventArgs e)
    {
        // Then, when you check the value of aaxe, it will = "y".
        if (aaxe == null)
        {
            richTextBox1.AppendText("You Don't Have An Axe\r\n");
        }
        if (aaxe == "y")
        {
            richTextBox1.AppendText("You Used your Axe\r\n");
        }
    }
}

你在不同的作用域中声明了两次变量 aaxe :一个是成员级别,另一个是局部变量。

请将第二个字符串aaxe改为aaxe = "y";没有字符串类型。

 string aaxe = "y";

  aaxe = "y";