2014-10-11 20 views
0

我真的是C#的新手。我已经尝试了好几天了解如何在我的计算器中使用单选按钮。我正在制作一个通过选择单选按钮来工作的计算器。在计算器中使用单选按钮

我已经尝试了从教程到教科书的所有内容。

希望你能帮助我。

这是我失败的代码中的一个

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

     private void button1_Click(object sender, EventArgs e) 
     { 
      int x, 
       y; 

      x = Convert.ToInt16(textBox1.Text); 
      y = Convert.ToInt16(textBox2.Text); 

      if (radioButton1.Checked) ; 

      Math.Pow(x,y); 

      if (radioButton2.Checked) ; 
      (x/y); 

      if (radioButton3.Cheked) ; 

     } 
    } 
} 

在这种情况下,我百达得到的错误

错误1只分配,调用,递增,递减,并且可以使用新的对象表达式作为声明

我真的不知道该怎么做。

+0

在哪条线上出现错误? – SJD 2014-10-11 10:18:33

回答

0

您需要分配结果的一些变量或文本框(如果有的话):

txtboxResult.Text = Math.Pow(x,y).ToString(); 

其实有很多的问题。

private void button1_Click(object sender, EventArgs e) 
{ 
    int x, 
     y; 

    x = Convert.ToInt16(textBox1.Text); 
    y = Convert.ToInt16(textBox2.Text); 

    if (radioButton1.IsChecked) //removed `;`, it refers to the empty if block and 'IsChecked' is a property not 'Checked' 
     txtboxResult.Text = Math.Pow(x,y).ToString(); //assign result to a textbox or may be a variable 

    if (radioButton2.IsChecked) //removed `;` 
     (x/y); 

    if (radioButton3.IsChecked) ; 
} 
0

你应该assing计算的结果为一些变量

private void button1_Click(object sender, EventArgs e) 
{ 
    double result; 
    int x, y; 

    x = Convert.ToInt16(textBox1.Text); 
    y = Convert.ToInt16(textBox2.Text); 

    if (radioButton1.Checked) 
     result = Math.Pow(x,y); 

    if (radioButton2.Checked) 
     result = (x/y); 

    //... 

} 
0

Checked是一个事件,而不是一个属性(当单选按钮得到遏制的情况下解雇)。该物业是IsChecked

if(radioButton1.IsChecked.GetValueOrDefault(false)) 
    ... 
0

我认为错误是在此声明

if (radioButton2.IsChecked) 
     (x/y); 

它应该是这样的

if (radioButton2.IsChecked) 
{ 
    txtboxResult.Text = Convert.toString(x/y); 
} 
0

填充结果如下另一个文本框。

int x,y; 

    double res = 0.0; 

    x = Convert.ToInt16(textBox1.Text); 
    y = Convert.ToInt16(textBox2.Text); 

    if (radioButton1.Checked) 

     res = Math.Pow(x, y); 

    if (radioButton2.Checked) 
     res = (x/y); 

    if (radioButton3.Checked) ; 

    textBox3.Text = res.ToString();