Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算器应用程序崩溃_C# - Fatal编程技术网

C# 计算器应用程序崩溃

C# 计算器应用程序崩溃,c#,C#,我正在尝试用C#构建一个简单的计算器应用程序,我不知道为什么在执行以下步骤时它会崩溃 输入0.2 单击减法 输入0 应用程序立即崩溃。我假设这与Zero()函数有关,因为单击Zero按钮时会执行Zero()函数。条件语句用于处理不应该出现的实例,如连续密码等。这是源代码。顺便说一下,其他数字的功能是相同的 public partial class MainWindow : Window { protected double firstNumber, secondN

我正在尝试用C#构建一个简单的计算器应用程序,我不知道为什么在执行以下步骤时它会崩溃

  • 输入0.2
  • 单击减法
  • 输入0
  • 应用程序立即崩溃。我假设这与Zero()函数有关,因为单击Zero按钮时会执行Zero()函数。条件语句用于处理不应该出现的实例,如连续密码等。这是源代码。顺便说一下,其他数字的功能是相同的

        public partial class MainWindow : Window
        {
            protected double firstNumber, secondNumber;
            protected string textBoxContents;
            protected int selectedFunction;
            public MainWindow()
            {
                InitializeComponent();
                firstNumber = 0;
                secondNumber = 0;
                selectedFunction = 0;
                textBoxContents = "0";
            }
            private void Zero(object sender, RoutedEventArgs e)
            {
                if (Convert.ToDouble(textBoxContents) > 0 || textBoxContents[textBoxContents.Length - 1] == '.')
                {
                    if(selectedFunction != 0)
                        textBoxContents = textBoxContents + "0";
                }
                else if (textBoxContents == null)
                {
                    textBoxContents = textBoxContents + "0";
                }
                ResultBox.Content = textBoxContents;
            }
            private void One(object sender, RoutedEventArgs e)
            {
                textBoxContents = textBoxContents + "1";
                ResultBox.Content = textBoxContents;
            }
            private void Decimal(object sender, RoutedEventArgs e)
            {
                textBoxContents = textBoxContents + ".";
                ResultBox.Content = textBoxContents;
            }
            private void Addition(object sender, RoutedEventArgs e)
            {
                firstNumber = Convert.ToDouble(textBoxContents);
                textBoxContents = null;
                selectedFunction = 1;
            }
            private void Subtraction(object sender, RoutedEventArgs e)
            {
                firstNumber = Convert.ToDouble(textBoxContents);
                textBoxContents = null;
                selectedFunction = 2;
            }
            private void Multiplication(object sender, RoutedEventArgs e)
            {
                firstNumber = Convert.ToDouble(textBoxContents);
                textBoxContents = null;
                selectedFunction = 3;
            }
            private void Division(object sender, RoutedEventArgs e)
            {
                firstNumber = Convert.ToDouble(textBoxContents);
                textBoxContents = null;
                selectedFunction = 4;
            }
            private void Result(object sender, RoutedEventArgs e)
            {
                secondNumber = Convert.ToDouble(textBoxContents);
                double thirdNumber = 0;
                switch (selectedFunction)
                {
                    case 1:
                        thirdNumber = firstNumber + secondNumber;
                        break;
                    case 2:
                        thirdNumber = firstNumber - secondNumber;
                        break;
                    case 3:
                        thirdNumber = firstNumber * secondNumber;
                        break;
                    case 4:
                        thirdNumber = firstNumber / secondNumber;
                        break;
                    default:
                        break;
                }
                textBoxContents = Convert.ToString(thirdNumber);
                ResultBox.Content = textBoxContents;
            }
            private void ClearEverything(object sender, RoutedEventArgs e)
            {
                textBoxContents = null;
                firstNumber = 0;
                secondNumber = 0;
                selectedFunction = 1;
                ResultBox.Content = Convert.ToString(0);
            }
            private void ToggleNegative(object sender, RoutedEventArgs e)
            {
                if (Convert.ToDouble(textBoxContents) != 0)
                {
                    textBoxContents = Convert.ToString(Convert.ToDouble(textBoxContents) * -1);
                    ResultBox.Content = textBoxContents;
                }
                else
                    ResultBox.Content = Convert.ToString(0);
            }
        }
    

    小数分隔符已本地化,是否确实使用了正确的区域性(“,”而不是“.”)


    如果这是问题所在,请查看此

    十进制分隔符已本地化,是否确定使用了正确的区域性(“,”而不是“)


    如果这是问题所在,请查看此

    十进制分隔符已本地化,是否确定使用了正确的区域性(“,”而不是“)


    如果这是问题所在,请查看此

    十进制分隔符已本地化,是否确定使用了正确的区域性(“,”而不是“)


    如果这就是问题所在,请在您正在执行的减法函数中查看此项

    textBoxContents=null

    然后在零度,你有

    textBoxContents[textBoxContents.Length-1]

    这就是它崩溃的原因


    在对减法函数中的textBoxContents执行任何操作之前,应先检查null

    private void Zero(object sender, RoutedEventArgs e)
    {
        if (Convert.ToDouble(textBoxContents) > 0 ||
            textBoxContents[textBoxContents.Length - 1] == '.')
        {
            if(selectedFunction != 0)
                textBoxContents = textBoxContents + "0";
        }
        else if (textBoxContents == null)
        {
            textBoxContents = textBoxContents + "0";
        }
        ResultBox.Content = textBoxContents;
    }
    
    textBoxContents=null

    然后在零度,你有

    textBoxContents[textBoxContents.Length-1]

    这就是它崩溃的原因


    在对减法函数中的textBoxContents执行任何操作之前,应先检查null

    private void Zero(object sender, RoutedEventArgs e)
    {
        if (Convert.ToDouble(textBoxContents) > 0 ||
            textBoxContents[textBoxContents.Length - 1] == '.')
        {
            if(selectedFunction != 0)
                textBoxContents = textBoxContents + "0";
        }
        else if (textBoxContents == null)
        {
            textBoxContents = textBoxContents + "0";
        }
        ResultBox.Content = textBoxContents;
    }
    
    textBoxContents=null

    然后在零度,你有

    textBoxContents[textBoxContents.Length-1]

    这就是它崩溃的原因


    在对减法函数中的textBoxContents执行任何操作之前,应先检查null

    private void Zero(object sender, RoutedEventArgs e)
    {
        if (Convert.ToDouble(textBoxContents) > 0 ||
            textBoxContents[textBoxContents.Length - 1] == '.')
        {
            if(selectedFunction != 0)
                textBoxContents = textBoxContents + "0";
        }
        else if (textBoxContents == null)
        {
            textBoxContents = textBoxContents + "0";
        }
        ResultBox.Content = textBoxContents;
    }
    
    textBoxContents=null

    然后在零度,你有

    textBoxContents[textBoxContents.Length-1]

    这就是它崩溃的原因

    在对textBoxContents执行任何操作之前,应先检查null

    private void Zero(object sender, RoutedEventArgs e)
    {
        if (Convert.ToDouble(textBoxContents) > 0 ||
            textBoxContents[textBoxContents.Length - 1] == '.')
        {
            if(selectedFunction != 0)
                textBoxContents = textBoxContents + "0";
        }
        else if (textBoxContents == null)
        {
            textBoxContents = textBoxContents + "0";
        }
        ResultBox.Content = textBoxContents;
    }
    
    这一逻辑似乎是一个线索。如果文本框的值为空,那么它将因为
    |
    另一侧的索引器而爆炸。我认为这可以改写为:

    private void Zero(object sender, RoutedEventArgs e)
    {
        var dblVal = Convert.ToDouble(textBoxContents.Text);
        textBoxContents.Text = dblVal.ToString();
        ResultBox.Content = textBoxContents.Text;
    }
    
    换句话说,如果文本框为空,则转换将产生
    0.0
    ;如果它以
    1结尾。
    它将产生
    1.0
    ;如果它是
    .5
    ,它将产生
    0.5
    。只需利用
    转换

    这一逻辑似乎是一个线索。如果文本框的值为空,那么它将因为
    |
    另一侧的索引器而爆炸。我认为这可以改写为:

    private void Zero(object sender, RoutedEventArgs e)
    {
        var dblVal = Convert.ToDouble(textBoxContents.Text);
        textBoxContents.Text = dblVal.ToString();
        ResultBox.Content = textBoxContents.Text;
    }
    
    换句话说,如果文本框为空,则转换将产生
    0.0
    ;如果它以
    1结尾。
    它将产生
    1.0
    ;如果它是
    .5
    ,它将产生
    0.5
    。只需利用
    转换

    这一逻辑似乎是一个线索。如果文本框的值为空,那么它将因为
    |
    另一侧的索引器而爆炸。我认为这可以改写为:

    private void Zero(object sender, RoutedEventArgs e)
    {
        var dblVal = Convert.ToDouble(textBoxContents.Text);
        textBoxContents.Text = dblVal.ToString();
        ResultBox.Content = textBoxContents.Text;
    }
    
    换句话说,如果文本框为空,则转换将产生
    0.0
    ;如果它以
    1结尾。
    它将产生
    1.0
    ;如果它是
    .5
    ,它将产生
    0.5
    。只需利用
    转换

    这一逻辑似乎是一个线索。如果文本框的值为空,那么它将因为
    |
    另一侧的索引器而爆炸。我认为这可以改写为:

    private void Zero(object sender, RoutedEventArgs e)
    {
        var dblVal = Convert.ToDouble(textBoxContents.Text);
        textBoxContents.Text = dblVal.ToString();
        ResultBox.Content = textBoxContents.Text;
    }
    

    换句话说,如果文本框为空,则转换将产生
    0.0
    ;如果它以
    1结尾。
    它将产生
    1.0
    ;如果它是
    .5
    ,它将产生
    0.5
    。只需利用
    转换

    文本框单击减法按钮后内容为空。 而不是
    textBoxContents=null使用
    textBoxContents=“0”
    textBoxContents=string.Empty。为什么要将其设置为空

    在Zero方法中调用
    textBoxContents.Length
    会导致
    NullReferenceException


    正如前面其他人提到的,Zero()中的逻辑有点迂回,当然可能更小。

    textBoxContents在单击减法按钮后为空。 而不是
    textBoxContents=null使用
    textBoxContents=“0”
    textBoxContents=string.Empty。为什么要将其设置为空

    在Zero方法中调用
    textBoxContents.Length
    会导致
    NullReferenceException


    正如前面其他人提到的,Zero()中的逻辑有点迂回,当然可能更小。

    textBoxContents在单击减法按钮后为空。 而不是
    textBoxContents=null使用
    textBoxContents=“0”
    textBoxContents=string.Empty。为什么要将其设置为空

    在Zero方法中调用
    textBoxContents.Length
    会导致
    NullReferenceException


    正如前面其他人提到的,Zero()中的逻辑有点迂回,当然可能更小。

    textBoxContents在单击减法按钮后为空。 而不是
    textBoxContents=null使用
    textBoxContents=“0”
    textBoxContents=string.Empty。为什么要将其设置为空

    在Zero方法中调用
    textBoxContents.Length
    会导致
    NullReferenceException

    正如前面其他人提到的,Zero()中的逻辑有点迂回,当然可能很小