我的计算器代码无效 [英] my calculator code not working

查看:105
本文介绍了我的计算器代码无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行代码时出现此错误。

I have this error when I try to run the code.

public partial class Calculator : Form
 {

    public Calculator()

    {
       InitializeComponent();

    }


    private void button1_Click(object sender, EventArgs e)
    {
       double a, b, r;
      string result = textBox4.Text.ToString();
       string operand = textBox2.Text.ToString();
       {

         switch ( result )
         {


          case " + " :
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a+b;
             textBox4.Text = result.ToString();
             break;

          case " - ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a-b;
             textBox4.Text = result.ToString();
             break;

           case " * ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a*b;
             textBox4.Text = result.ToString();
             break;

          case " / ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a/b;
             textBox4.Text = result.ToString();
             break;

          default:
             result = "This is not an operand";
             break;

         }

       }

    }
 }





TextBox1 =第一个数字

TextBox3 =第二个数字

TextBox2 =操作数

TextBox4 =结果

button1 =结果

非常感谢任何帮助。



谢谢。



TextBox1=first number
TextBox3=second number
TextBox2=operand
TextBox4=result
button1=result
Any help would be greatly appreciated.

Thank you.

推荐答案

注意:操作数menas你所乘的数字不是你在它们上面执行的操作。所以我将操作数改为操作



NOTE: operand menas the digits you are multiplying not the operation youa re performing on them. So i changed operand to operation

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

    private void button1_Click(object sender, EventArgs e)
    {
       double a, b, r;
     // string result = textBox4.Text.ToString();
       string Operation = textBox2.Text.ToString();
       {
 
         switch ( Operation )
         {
 

          case " + " :
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a+b;
             //textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          case " - ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a-b;
            // textBox4.Text = result.ToString();
            textBox4.Text = r.ToString();
             break;
 
           case " * ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a*b;
            // textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          case " / ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a/b;
             //textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          default:
             result = "This is not an operand";
             break;
 
         }
 
       }
 
    }
 }





我没有测试过代码,但是单击按钮就像在TextBox4(操作数),textBox3(操作数)和textbox2(操作)的基础上在textbox4上生成结果一样简单。



I have not tested the code but its as simple as clicking on the button will generate your result on textbox4 on the basis of TextBox1 (operand),textBox3(operand) and textbox2(operation).


这篇关于我的计算器代码无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆