【问题标题】:C# always false bool [closed]C#总是假布尔[关闭]
【发布时间】:2013-12-09 08:44:30
【问题描述】:

有人可以帮忙吗?我无法弄清楚为什么当我按下 button7 时 bool floorOne 总是设置为 false,即使我先按下 button3 或 button1。这应该是一个相当简单的问题,只有在初始化、按下按钮 2 或按下按钮 4 时才会变为假。我不知道它是如何返回 false 的。

它可能有一个相对简单的解决方案,但我找不到它,谢谢你的时间。

编辑:当我调试时,它显示为 false,我不知道该信息是否会有所帮助。我知道很多代码可能不需要包含在此处,但以防万一其中出现问题,我认为我应该将其添加进去。

Edit2:太棒了,非常感谢大家!

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Elevator
{
    public partial class Form1 : Form
    {
        public bool doorsOpen;
        public bool floorOne;
        public bool groundFloor;
        public Form1()
        {
            InitializeComponent();

            pictureBox1.Visible = false; // This is the bottom floor doors closed picture
            pictureBox2.Visible = true; // This is the bottom floor doors open picture
            pictureBox3.Visible = false; // This is the top floor doors closed picture
            pictureBox4.Visible = false; // This is the top floor doors open picture
            floorOne = false;
            richTextBox1.Text = "Ground floor";
            button1.BackColor = Color.Gray;
            button2.BackColor = Color.Gray;
            button3.Enabled = true;
            button4.Enabled = false; // This makes it impossible to click the buttons if
            button5.Enabled = false; // the lift is already on that floor, to start with this is the
            button6.Enabled = true;  // ground floor.
            button5.BackColor = Color.Black;
            button6.BackColor = Color.Red;
            doorsOpen = true;
            richTextBox2.Text = "Doors open";
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void button3_Click(object sender, EventArgs e)
        {
            if (doorsOpen == true)
            {
                doorsOpen = false;
                richTextBox2.Text = "Doors closed";
            }
            pictureBox1.Visible = false; // This is the bottom floor doors closed picture
            pictureBox2.Visible = false; // This is the bottom floor doors open picture
            pictureBox3.Visible = true; // This is the top floor doors closed picture
            pictureBox4.Visible = false; // This is the top floor doors open picture
            bool floorOne = true;
            button1.BackColor = Color.Gray;
            button2.BackColor = Color.Gray;
            richTextBox1.Text = "First floor";
            button3.Enabled = false;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = false;
            button5.BackColor = Color.Red;
            button6.BackColor = Color.Black;
        }

        public void button4_Click(object sender, EventArgs e)
        {
            if (doorsOpen == true)
            {
                doorsOpen = false;
                richTextBox2.Text = "Doors closed";
            }
            pictureBox1.Visible = true; // This is the bottom floor doors closed picture
            pictureBox2.Visible = false; // This is the bottom floor doors open picture
            pictureBox3.Visible = false; // This is the top floor doors closed picture
            pictureBox4.Visible = false; // This is the top floor doors open picture
            bool floorOne = false;
            button1.BackColor = Color.Gray;
            button2.BackColor = Color.Gray;
            richTextBox1.Text = "Ground floor";
            button3.Enabled = true;
            button4.Enabled = false;
            button5.Enabled = false; 
            button6.Enabled = true;  
            button5.BackColor = Color.Black;
            button6.BackColor = Color.Red;
        }

        public void button7_Click(object sender, EventArgs e)
        {
            doorsOpen = true;
            richTextBox2.Text = "Doors open";
           if (floorOne == true)
           { 
            pictureBox1.Visible = false; // This is the bottom floor doors closed picture
            pictureBox2.Visible = false; // This is the bottom floor doors open picture
            pictureBox3.Visible = false; // This is the top floor doors closed picture
            pictureBox4.Visible = true; // This is the top floor doors open picture
           }
           else if (floorOne != true)
           {
               pictureBox1.Visible = false; // This is the bottom floor doors closed picture
               pictureBox2.Visible = true; // This is the bottom floor doors open picture
               pictureBox3.Visible = false; // This is the top floor doors closed picture
               pictureBox4.Visible = false; // This is the top floor doors open picture
           }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            if (doorsOpen == true)
            {
                doorsOpen = false;
                richTextBox2.Text = "Doors closed";
            }
            pictureBox1.Visible = false; // This is the bottom floor doors closed picture
            pictureBox2.Visible = false; // This is the bottom floor doors open picture
            pictureBox3.Visible = true; // This is the top floor doors closed picture
            pictureBox4.Visible = false; // This is the top floor doors open picture
            bool floorOne = true;
            button1.BackColor = Color.Yellow;
            button2.BackColor = Color.Gray;
            richTextBox1.Text = "First floor";
            button3.Enabled = false;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = false;
            button5.BackColor = Color.Red;
            button6.BackColor = Color.Black;

        }

        public void button2_Click(object sender, EventArgs e)
        {
            if (doorsOpen == true)
            {
                doorsOpen = false;
                richTextBox2.Text = "Doors closed";
            }
            pictureBox1.Visible = true; // This is the bottom floor doors closed picture
            pictureBox2.Visible = false; // This is the bottom floor doors open picture
            pictureBox3.Visible = false; // This is the top floor doors closed picture
            pictureBox4.Visible = false; // This is the top floor doors open picture
            bool floorOne = false;
            button1.BackColor = Color.Gray;
            button2.BackColor = Color.Yellow;
            richTextBox1.Text = "Ground floor";
            button3.Enabled = true;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = true;
            button5.BackColor = Color.Black;
            button6.BackColor = Color.Red;
        }
     }
}

【问题讨论】:

  • 旁注if (floorOne == true) ... else if (floorOne != true) - 你确定你需要检查一楼是不是真的,如果不是真的?完全相同应使用if (floorOne) .. else 进行验证
  • 你真的应该更好地命名你的变量和控件。
  • 你有时应该使用调试器...

标签: c# boolean


【解决方案1】:

因为在您的某些方法中,您声明了一个 new floorOne 变量,而不是修改现有 Form1 的字段。

替换

bool floorOne = true;

floorOne = true;

【讨论】:

    【解决方案2】:

    在 Button3 和 button4 的点击事件中,删除 bool 变量的声明,只更新它的值。因为您已经定义了 bool floorOne,所以您不需要再次声明它。

    更新

    bool floorOne = true;
    

    收件人

    floorOne = true;
    

    【讨论】:

      【解决方案3】:

      这确实有一个简单的解决方案。 可以说,您单击 button1 并希望将 floorOne 设置为 true,但您真正要做的是: 创建新的本地布尔值 floorOne 并将其设置为 true (bool floorOne = true;)。但这不会改变您的全球价值 floorOne。它具有相同的名称,但在这种情况下并不重要。 删除 button1 代码中的“bool”文本,然后“floorOne”将成为您的全局值。

      button1 的新代码(所有其他按钮的更改相同):

      public void button1_Click(object sender, EventArgs e)
          {
              if (doorsOpen == true)
              {
                  doorsOpen = false;
                  richTextBox2.Text = "Doors closed";
              }
              pictureBox1.Visible = false; // This is the bottom floor doors closed picture
              pictureBox2.Visible = false; // This is the bottom floor doors open picture
              pictureBox3.Visible = true; // This is the top floor doors closed picture
              pictureBox4.Visible = false; // This is the top floor doors open picture
              floorOne = true; // HERE IS THE CHANGE
              button1.BackColor = Color.Yellow;
              button2.BackColor = Color.Gray;
              richTextBox1.Text = "First floor";
              button3.Enabled = false;
              button4.Enabled = true;
              button5.Enabled = true;
              button6.Enabled = false;
              button5.BackColor = Color.Red;
              button6.BackColor = Color.Black;
          }
      

      试试这个:

      public partial class Form1 : Form
      {
          public bool floorOne; // global value (automatically set to false (default value of false)
      
          public Form1()
          {
              InitializeComponent();
          }
      
          public void button3_Click(object sender, EventArgs e)
          {
              bool floorOne = true; // new local value named floorOne (doesn change your global value)
              MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString());
              // this.floorOne is your global value (try to find something about "this.")
              this.floorOne = true;
              MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 2023-03-31
        相关资源
        最近更新 更多