计算器运行时错误

时间:2013-09-18 21:21:07

标签: c# runtime calculator

今天早上我创建了一个Windows窗体项目,根据我学到的东西制作一个计算器,并自学一些编程。

一切都很好,我可以让数字按钮在文本框中打印特定的数字,操作员按钮清除文本框,这样你就可以写第二个数字,但当我按下等于“=”时,我得到这个运行时间错误:

  

值不能为空。参数名称:String

我将向您展示form1类中的代码(通常它是我必须编辑的唯一类):

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

    List<string> theNumberList = new List<string>();

    public string numbers = null;

    int number1, number2;
    string num1, num2, operat;

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        theNumberList.Add("1");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    public static void ShowNumbers(List<string> theList, string aString, TextBox textBox)
    {
        foreach(string number in theList)
        {
            aString = aString + number;
            textBox.Text = aString;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        theNumberList.Add("2");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        theNumberList.Add("3");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        theNumberList.Add("4");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        theNumberList.Add("5");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        theNumberList.Add("6");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button7_Click(object sender, EventArgs e)
    {
        theNumberList.Add("7");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button8_Click(object sender, EventArgs e)
    {
        theNumberList.Add("8");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button9_Click(object sender, EventArgs e)
    {
        theNumberList.Add("9");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button0_Click(object sender, EventArgs e)
    {
        theNumberList.Add("0");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void Plus_Click(object sender, EventArgs e)
    {
        num1 = numbers;
        theNumberList.Clear();
        num2 = numbers;
        operat = "+";
    }

    private void Subtract_Click(object sender, EventArgs e)
    {

    }

    private void Multiply_Click(object sender, EventArgs e)
    {

    }

    private void Divide_Click(object sender, EventArgs e)
    {

    }

    private void Equal_Click(object sender, EventArgs e)
    {
        int result;
        switch (operat)
        {
            case "+":
                number1 = int.Parse(num1);
                number2 = int.Parse(num2);
                result = number1 + number2;
                theNumberList.Clear();
                textBox1.Text = result.ToString();
                break;
        }
    }
}

所以这是代码,错误来自Equal_click“number1 = int.parse(num1);”在这里:

private void Equal_Click(object sender, EventArgs e)
    {
        int result;
        switch (operat)
        {
            case "+":
                number1 = int.Parse(num1);
                number2 = int.Parse(num2);
                result = number1 + number2;
                theNumberList.Clear();
                textBox1.Text = result.ToString();
                break;
        }
    }

我不知道如何解决它并出现了什么问题?

3 个答案:

答案 0 :(得分:1)

您收到异常,因为num1为空。在这个功能中:

public static void ShowNumbers(List<string> theList, string aString, TextBox textBox)
{
    foreach(string number in theList)
    {
        aString = aString + number;
        textBox.Text = aString;
    }
}

您正在设置aString的值,但不是numbers。请参阅this answer了解为什么以这种方式传递字符串不起作用。您需要将其更改为使用numbers变量。注意我们也使它成为一个成员函数而不是静态:

public void ShowNumbers()
{
    foreach(string number in theNumberList)
    {
        numbers = numbers + number;
        textBox1.Text = numbers;
    }
}

此外,还有另一个问题。当您按num2时,按=时,您需要指定+

答案 1 :(得分:0)

这意味着你的num1很可能是空的,它无法将空字符串转换为数字。

答案 2 :(得分:0)

int.Parse可以解析数字模式中的字符串,如=&gt; “9” 所以当num1为null或num1 =“”时; int.parse,无法将其解析为int

试试这个

而不是 Parse 你应该使用 TryParse

int.TryParse(string,out int)