-1

当我调试代码时,第一个数字正在正确执行该功能,但是当我单击按钮“=”然后结果出现在文本框中,当我用来单击另一个按钮时,数字从结果继续而不是清除字段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CACalculator
{
    public partial class Form1 : Form
    {
        double num1, num2, result;
        string op;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn9_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text + btn9.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn8.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn7.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn6.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn5.Text;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn4.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn3.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn2.Text;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn1.Text;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn0.Text;
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnadd.Text;
        }

        private void btnsub_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnsub.Text;
        }

        private void btnmul_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnmul.Text;
        }

        private void btndiv_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btndiv.Text;
        }

        private void btnquo_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnquo.Text;
        }

        private void btneql_Click(object sender, EventArgs e)
        {
            num2 = Convert.ToDouble(txtnum.Text);
            switch (op)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    result = num1 / num2;
                    break;
                case "%":
                    result = num1 % num2;
                    break;
            }
            txtnum.Text = result.ToString();
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            txtnum.Clear();
        }
    }

}
4

1 回答 1

1

您必须清除 Num1 和 num2 aswel 作为文本框,如下所示:

    private void btnC_Click(object sender, EventArgs e)
    {
        txtnum.Clear();
        num1 = 0;    
        num2 = 0;   //clear all the numbers used in the calculator.
        result = 0; 
    }

否则,您将清除视觉部分,但不会清除其背后的数字,您实际上正在计算这些数字

让我知道它是否有帮助。

于 2013-09-11T06:09:42.133 回答