1

我到底如何设置尝试和捕获来阻止用户输入超过一位小数。这是一个尝试和捕捉吗?或者我到底想在这里做什么?对 Windows 窗体应用程序非常陌生......

还有一点需要注意......当我点击计算时,数字消失了......它不会一直留在那里。我觉得这很奇怪。有人会知道为什么吗?例如,如果我点击 6 + 6,它会显示“6”,然后是另一个“6”,然后是 12,而不是 6 + 6 = 12 的显示。我也不明白

编辑:这是一个更新的代码。我拥有一切,除了我仍然不明白如何连续编写代码。欢迎任何建议,因为我被卡住了......

public partial class Form1 : Form
{
    char c;
    double num1;
    double num2;

    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text += 0;
    }
    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text += 1;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text += 2;
    }
    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text += 3;
    }
    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text += 4;
    }
    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text += 5;
    }
    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text += 6;
    }
    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text += 7;
    }
    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text += 8;
    }
    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text += 9;
    }
    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains("."))
            txtBox.Text += ".";
    }
    private void btnAddition_Click(object sender, EventArgs e)
    {
        c = '+';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        c = '-';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        c = '*';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnDivision_Click(object sender, EventArgs e)
    {
        c = '/';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {
        num2 = double.Parse(txtBox.Text);
        double result;
        switch (c)
        {
            case '+':
                result = num1 + num2;
                txtBox.Text = result.ToString();
                break;
            case '-':
                result = num1 - num2;
                txtBox.Text = result.ToString();
                break;
            case '/':
                 if (num2!= 0) 
                {
                    result = num1 / num2;
                    txtBox.Text = result.ToString();
                } 
                else 
                {
                    txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
                } 
                break; 
            case '*':
                result = num1 * num2;
                txtBox.Text = result.ToString();
                break;
        }
    }
}

}

4

2 回答 2

3

首先,您不需要 try catch 语句。try catch 语句用于异常处理。

清除后,您想要的是一种将当前输入添加到您保存在内存中的数字并将其显示在屏幕上的单一方法。我不会向您展示代码,因为您有很多要更改的地方,但基本上您应该看一个计算器示例

于 2013-09-01T05:45:09.693 回答
2

是的,您不需要 Try catch Block 它进行异常处理,并且您必须更改您的代码,所以您可以尝试这个它可以帮助您............

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 SimpleCalculator 
{ 
    public partial class frmCalculator : Form 
    { 
        string operand1 = string.Empty; 
        string operand2 = string.Empty; 
        string result; 
        char operation; 

        public frmCalculator() 
        { 
            InitializeComponent(); 
        } 

        private void frmCalculator_Load(object sender, EventArgs e) 
        { 
            btnOne.Click += new EventHandler(btn_Click); 
            btnTwo.Click += new EventHandler(btn_Click); 
            btnThree.Click += new EventHandler(btn_Click); 
            btnFour.Click += new EventHandler(btn_Click); 
            btnFive.Click += new EventHandler(btn_Click); 
            btnSix.Click += new EventHandler(btn_Click); 
            btnSeven.Click += new EventHandler(btn_Click); 
            btnEight.Click += new EventHandler(btn_Click); 
            btnNine.Click += new EventHandler(btn_Click); 
            btnZero.Click += new EventHandler(btn_Click); 
            btnDot.Click += new EventHandler(btn_Click); 
        } 

        void btn_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                Button btn = sender as Button; 

                switch (btn.Name) 
                { 
                    case "btnOne": 
                        txtInput.Text += "1"; 
                        break; 
                    case "btnTwo": 
                        txtInput.Text += "2"; 
                        break; 
                    case "btnThree": 
                        txtInput.Text += "3"; 
                        break; 
                    case "btnFour": 
                        txtInput.Text += "4"; 
                        break; 
                    case "btnFive": 
                        txtInput.Text += "5"; 
                        break; 
                    case "btnSix": 
                        txtInput.Text += "6"; 
                        break; 
                    case "btnSeven": 
                        txtInput.Text += "7"; 
                        break; 
                    case "btnEight": 
                        txtInput.Text += "8"; 
                        break; 
                    case "btnNine": 
                        txtInput.Text += "9"; 
                        break; 
                    case "btnZero": 
                        txtInput.Text += "0"; 
                        break; 
                    case "btnDot": 
                        if(!txtInput.Text.Contains(".")) 
                            txtInput.Text += "."; 
                        break; 

                } 
            } 
            catch(Exception ex) 
            { 
                MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details: " + 
                    ex.Message); 
            } 
        } 

        private void txtInput_KeyPress(object sender, KeyPressEventArgs e) 
        { 
            switch (e.KeyChar) 
            { 
                case '1': 
                case '2': 
                case '3': 
                case '4': 
                case '5': 
                case '6': 
                case '7': 
                case '8': 
                case '9': 
                case '0': 
                //case '+': 
                //case '-': 
                //case '*': 
                //case '/': 
                //case '.': 
                    break; 
                default: 
                    e.Handled = true; 
                    MessageBox.Show("Only numbers, +, -, ., *, / are allowed"); 
                    break; 
            }            
        } 

        private void txtInput_TextChanged(object sender, EventArgs e) 
        { 

        } 

        private void btnPlus_Click(object sender, EventArgs e) 
        { 
            operand1 = txtInput.Text; 
            operation = '+'; 
            txtInput.Text = string.Empty; 
        } 

        private void btnMinus_Click(object sender, EventArgs e) 
        { 
            operand1 = txtInput.Text; 
            operation = '-'; 
            txtInput.Text = string.Empty; 
        } 

        private void btnMulitply_Click(object sender, EventArgs e) 
        { 
            operand1 = txtInput.Text; 
            operation = '*'; 
            txtInput.Text = string.Empty; 
        } 

        private void btnDivide_Click(object sender, EventArgs e) 
        { 
            operand1 = txtInput.Text; 
            operation = '/'; 
            txtInput.Text = string.Empty; 
        } 

        private void btnEqual_Click(object sender, EventArgs e) 
        { 
            operand2 = txtInput.Text; 

            double opr1, opr2; 
            double.TryParse(operand1, out opr1); 
            double.TryParse(operand2, out opr2); 

            switch (operation) 
            { 
                case '+': 
                    result = (opr1 + opr2).ToString(); 
                    break; 

                case '-': 
                    result = (opr1 - opr2).ToString(); 
                    break; 

                case '*': 
                    result = (opr1 * opr2).ToString(); 
                    break; 

                case '/': 
                    if (opr2 != 0) 
                    { 
                        result = (opr1 / opr2).ToString(); 
                    } 
                    else 
                    { 
                        MessageBox.Show("Can't divide by zero"); 
                    } 
                    break; 
            } 

            txtInput.Text = result.ToString(); 
        } 

        private void btnClear_Click(object sender, EventArgs e) 
        { 
            txtInput.Text = string.Empty; 
            operand1 = string.Empty; 
            operand2 = string.Empty; 
        } 

        private void btnSqrRoot_Click(object sender, EventArgs e) 
        { 
            double opr1; 
            if (double.TryParse(txtInput.Text, out opr1)) 
            { 
                txtInput.Text = (Math.Sqrt(opr1)).ToString(); 
            } 
        } 

        private void btnByTwo_Click(object sender, EventArgs e) 
        { 
            double opr1; 
            if (double.TryParse(txtInput.Text, out opr1)) 
            { 
                txtInput.Text = (opr1 / 2).ToString(); 
            } 
        } 

        private void btnByFour_Click(object sender, EventArgs e) 
        { 
            double opr1; 
            if (double.TryParse(txtInput.Text, out opr1)) 
            { 
                txtInput.Text = (opr1 / 4).ToString(); 
            } 
        } 
    } 
} 
于 2013-09-01T05:53:48.097 回答