2014-10-11 55 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();