2013-03-07 118 views
0

我的問題是,我的程序在這裏有按鈕21的功能之間循環的麻煩取決於if語句的寫法,如果功能工作,但其他不會。 在下面的代碼中,我已經將它設置爲button2,它的工作原理但是我希望按鈕1和2都使用button21,如果他們選擇的話。然而函數設置qw == 1是在這個程序中工作的不是qw == 2 那麼我的程序代碼有什麼問題? 代碼顯示:麻煩,如果循環

namespace Matematisk_indlæring 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     Random RND = new Random(Guid.NewGuid().GetHashCode()); 
     Random RND2 = new Random(Guid.NewGuid().GetHashCode()); 
     private void quitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      button1.Hide(); 
      button2.Hide(); 
      button3.Hide(); 
      button4.Hide(); 
      button5.Hide(); 
      label1.Show(); 
      textBox1.Show(); 
      button21.Show(); 
      double qw = 1; 
      textBox2.Text = qw.ToString(); 
      string q = "1+1"; 
      label1.Text = q; 
      int qq = 1 + 1; 
      textBox3.Text = qq.ToString(); 
     } 


     private void button21_Click(object sender, EventArgs e) 
     { 
      double qqq = Convert.ToDouble(textBox1.Text); 
      double qq = Convert.ToDouble(textBox3.Text); 
      int qw = Convert.ToInt32(textBox2.Text); 
      if (qw == 1) 
      { 
       if (qq == qqq) 
       { 

        MessageBox.Show("succes"); 
        int qws1; 

        int qws; 

        qws1 = RND2.Next(51, 100); 


        qws = RND.Next(0, 50); 

        qq = qws1 + qws; 
        textBox3.Text = qq.ToString(); 
        string tese = qws.ToString(); 
        string tese2 = qws1.ToString(); 
        label1.Text = tese2 + "+" + tese; 

       } 

        if (qw == 2) 
        { 
         if (qq == qqq) 
         { 

          MessageBox.Show("succes"); 
          int qws1; 

          int qws; 

          qws1 = RND2.Next(51, 100); 


          qws = RND.Next(0, 50); 

          qq = qws1 - qws; 
          textBox3.Text = qq.ToString(); 
          string tese = qws.ToString(); 
          string tese2 = qws1.ToString(); 
          label1.Text = tese2 + "-" + tese; 

         } 




       } 
      } 
     } 
     private void button2_Click(object sender, EventArgs e) 
     { 
      button1.Hide(); 
      button2.Hide(); 
      button3.Hide(); 
      button4.Hide(); 
      button5.Hide(); 
      label1.Show(); 
      textBox1.Show(); 
      button21.Show(); 
      double qw = 2; 
      textBox2.Text = qw.ToString(); 
       string q = "1-1"; 
       label1.Text = q; 
       int qq = 1 - 1; 
       textBox3.Text = qq.ToString(); 
     } 
    } 
} 
+0

歡迎Stackoverlfow,請閱讀[常見問題]和[提問]幾次.. – 2013-03-07 07:31:32

+0

酷:有人把C#的Unicode支持使用。 – nneonneo 2013-03-07 07:31:46

+0

@nneonneo:不酷:SO的語法突出顯示扼流圈:-( – 2013-03-07 07:33:25

回答

1

這裏至少有一個邏輯缺陷。

button21_Click:首先測試qw是否爲1,然後再測試它是否爲2,但沒有一個代碼更改爲qw。那麼它怎麼會突然從1變成2呢?

在此方法的代碼基本上是:

int qw = Convert.ToInt32(textBox2.Text); 

if (qw == 1) 
{ 
    if (qq == qqq) 
    { 
     // code which does not modify qw 
    } 

    if (qw == 2)   // wrong placement of this if-statement! 
    { 
     // code which can never be called! 
    } 
} 

因此,大家可以看到,控制不能用在此方法達到if (qw == 2)因爲你嵌套IFS的錯誤。你可以,如果你這樣更容易看到修復縮進。

另外:杜burde bruke engelsk klassenavn,IKKE丹麥語:-)

+0

即按鈕1和2的功能,他們都改變雙qw的可能結果因爲您可能會看到他們將編號寫入文本框,然後button21從文本框中截取該編號 – user2010884 2013-03-07 07:42:39

+0

s,但是你在'button21_Click'中嵌入了if語句錯誤.. – Grubl3r 2013-03-07 07:46:50

+0

請向我解釋嵌套的哪一部分我錯了,因爲我真的不能在我的設置中看到問題,因爲從文本框中的數字在所有內容之前被抓取其他?? – user2010884 2013-03-07 07:54:19