2013-12-09 66 views
-1

任何人都可以幫忙嗎?當我按下按鈕7時,即使先按下按鈕3或按鈕1,我也無法確定爲什麼布爾floorOne總是設置爲false。這應該是一個相當簡單的問題,只有在初始化時,按下按鈕2或按下按鈕4時,它纔會變爲false。我不知道它是如何返回到錯誤的。C#總是假布爾

它可能有一個相對簡單的解決方案,但我找不到它,謝謝你的時間。

編輯:當我調試時,它顯示爲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; 
     } 
    } 
} 
+3

邊注'如果(floorOne ==真)......否則,如果(floorOne != true)' - 你確定你需要檢查一樓是不是真的,如果不是真的?完全相同應該用'if(floorOne).. else'來驗證。 –

+3

你應該更好地爲你的變量和控件命名。 – Maarten

+0

有時你應該使用調試器... – giammin

回答

6

因爲你的一些方法你聲明,而不是修改現有Form1的領域floorOne變量。

更換

bool floorOne = true; 

floorOne = true; 
1

在將Button3和將Button4單擊事件刪除布爾變量的聲明,只是更新了它的價值。因爲你已經定義了布爾floorOne所以你不需要再次聲明它。

更新

bool floorOne = true; 

floorOne = true; 
0

這真的有一個簡單的解決方案。 假設你點擊了button1,並且想要將floorOne設置爲true,但是你真正要做的是: 創建新的LOCAL布爾值floorOne並將其設置爲true(bool floorOne = true;)。但是這不會改變你的GLOBAL價值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()); 
    } 
}