Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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#_Visual Studio 2019 - Fatal编程技术网

C# 获得;无效的表达式术语";使用+;=操作人员

C# 获得;无效的表达式术语";使用+;=操作人员,c#,visual-studio-2019,C#,Visual Studio 2019,我使用visualstudio。这是我一直在挣扎的代码: double answer = 0; private void button2_Click(object sender, EventArgs e) { { try { double value1 = Convert.ToDouble(this.textBox1.Text); double answer += value1; }

我使用visualstudio。这是我一直在挣扎的代码:

double answer = 0;

private void button2_Click(object sender, EventArgs e)
{
   {
        try
        {
            double value1 = Convert.ToDouble(this.textBox1.Text);
            double answer += value1;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error on input dummy", "Warning");
        }
     }
}
我已经创建了一个运行上述代码的按钮
我认为我已经正确设置了变量,但我不能完全确定。我需要另一个按钮来访问它,我不完全确定该怎么办


我需要知道的是,为什么第9行的
+=
给了我一个错误,以及我需要做什么更改来解决这个问题,因为我不确定。

当你说
双答案+=value1
您正在声明一个名为
answer
的变量。因为您已经在前面声明了它,所以您将得到一个错误

您只需添加值,而不需要像下面这样声明变量:

answer += value1;

用新行更改下面的行

double answer += value1;
double answer += value1;


既然您已经定义了
答案
,只需更改此行即可


您已经定义了答案变量。 下面的更正 注意:我建议使用double.TryParse

double answer;

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double value1;
                if (double.TryParse(this.textBox1.Text, out value1))
                {
                    answer += value1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error on input dummy", "Warning");
            }
        }

+=
添加现有变量的值。您正在这里声明一个新变量
answer
。。。它没有以前的值。你希望它有什么价值?如果您只是试图分配给字段,请删除失败行上的
double
,更改
double answer+=value1至<代码>答案+=值1。如果有任何答案对您有帮助,请。
answer += value1;
double answer;

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double value1;
                if (double.TryParse(this.textBox1.Text, out value1))
                {
                    answer += value1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error on input dummy", "Warning");
            }
        }