2014-02-17 113 views
0

我是编程新手,我试图制作一个简单的计算器,但使用单选按钮作为+ - * /按钮。该表单有两个文本框供用户使用,其间有单选按钮和用于答案的文本框。此代码有什么问题:单选按钮其他如果

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

    private void button1_Click(object sender, EventArgs e) 
    { 
     int itextBox1 = 0; 
     int itextBox2 = 0; 
     int itextBox3 = 0; 

     itextBox1 = Convert.ToInt32(textBox1.Text); 
     itextBox2 = Convert.ToInt32(textBox2.Text); 

     if (radioButton1.Checked) 
     { 
       itextBox3 = itextBox1 + itextBox2; 
     } 
     else if (radioButton2.Checked) 
     { 
      itextBox3 = itextBox1 - itextBox2; 
     } 
     else if (radioButton3.Checked) 
     { 
      itextBox3 = itextBox1 * itextBox2; 
     } 
     else if (radioButton4.Checked) 
     { 
      itextBox3 = itextBox1/itextBox2; 
     } 
    }//void 

}//class 
+3

你不这样做对你的成绩东西,一旦你计算。你只是将它粘在函数的局部变量中。 –

+0

为什么你使用单选按钮而不是普通的按钮?一个真正的计算器有正常的按钮... –

回答

3

您正在计算结果,但没有对它做任何处理。添加类似

textBox3.Text = itextBox3.ToString(); 

计算后。

2

你错过:

textBox3.Text = itextBox3.ToString(); 
+2

这将无法正常工作。 itextBox3是一个'int',它必须转换为'string'。 –

+0

你说得对。我刚刚更新了答案。 –

3

你可能需要补充一点:

textBox3.Text = itextBox3.ToString(); 

你调试代码?有什么问题。

空事件处理程序有什么意义?

3

问题:您没有在TextBox3上显示结果值。

试试这个:

itextBox3.Text=itextBox3.ToString(); 
2

你可以添加

MessageBox.Show(itextBox3.ToString()); 

,以显示你的结果

private void button1_Click(object sender, EventArgs e) 
{ 
    int itextBox1 = 0; 
    int itextBox2 = 0; 
    int itextBox3 = 0; 

    itextBox1 = Convert.ToInt32(textBox1.Text); 
    itextBox2 = Convert.ToInt32(textBox2.Text); 

    if (radioButton1.Checked) 
    { 
      itextBox3 = itextBox1 + itextBox2; 
    } 
    else if (radioButton2.Checked) 
    { 
     itextBox3 = itextBox1 - itextBox2; 
    } 
    else if (radioButton3.Checked) 
    { 
     itextBox3 = itextBox1 * itextBox2; 
    } 
    else if (radioButton4.Checked) 
    { 
     itextBox3 = itextBox1/itextBox2; 
    } 
    MessageBox.Show(itextBox3.ToString()); 
} 
相关问题