如何将多个变量的值添加到一个变量中? | C#

How to add values to multiple variables into one single variable? | C#

如何在C#中将多个变量的值添加到一个变量中?

比如我声明了三个变量

decimal number1 = 53m;

decimal number2 = 33m;

decimal total;    

现在,当我这样做的时候

total = number1 + number2; 

报错"use of unassigned local variable"

即使这样也行不通

total = number1;  
total = number2;    

也不行

total += number1 
total += number2

我知道在文本框中我们喜欢这样做,所以,我认为它也适用于变量,但事实并非如此。

totaltextbox.text += textbox.text1  
totaltextbox.text += textbox.text2  

EDIT:

在我将声明中的每个值分配给每个变量后,它现在可以工作了。

之前我只在 if 语句中分配它们,这导致了问题。看看我的方法,例如

private decimal OilLubeCharges()
        {
           decimal oilChange_var=0m;
           decimal lubeJob_var=0m;
           decimal oilLube_var=0m;
           decimal totalOiltLubeCharges_var=0m;


            if (oilChangeCheckBox.Checked)
            {
                oilChange_var = 26.00m;

            }

            else if (lubeJobCheckBox.Checked)
            {
                lubeJob_var = 18.00m;
            }

            else if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
            {
                oilLube_var = 26.00m + 18.00m;
            }

            totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;  

我不明白如果我在 if 语句中给了变量一个值那么它也应该工作。为什么他们需要一开始就给出一个值?

感谢所有付出努力的人。尊重!

Use of unassigned local variable means:

decimal total;  

This has been declared, but it has no value and you are trying to use it before it has a value. There is no assignment here.

So you initialise it:

decimal total = 0;

Your problem now goes away.

However, this will only be an error if you try and use an unintialised variable.

So technically:

decimal val1 = 10;
decimal val2 = 10;

decimal total;

total = val1 + val2; // this works

This is also the same as

decimal total = val1 + val2; // an assignment.

What won't work

decimal total; // compiler knows you want a total variable

if (total > 10) // at this point, there is no value in total so how can we compare it
{
    Console.WriteLine("This won't compile");
}

Looking at your problem

private decimal OilLubeCharges()
{
   // this is what i **think** you might have started with
   decimal oilChange_var;
   decimal lubeJob_var;
   decimal oilLube_var;
   decimal totalOiltLubeCharges_var;


    if (oilChangeCheckBox.Checked)
    {
        oilChange_var = 26.00m;

    }

    else if (lubeJobCheckBox.Checked)
    {
        lubeJob_var = 18.00m;
    }

    else if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
    {
        oilLube_var = 26.00m + 18.00m;
    }

    totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;

It is simple here, because you have defined you variables up top, you are then using if statements to define whether or not these variables get set. Which means there is a possibility that they wont have a value. This is because of this line

totalOiltLubeCharges_var = oilChange_var + lubeJob_var + oilLube_var;

Here you are categorically using all the variables. But before that line you're conditionally giving them an initial value.

So for instance if lubeJobCheckBox.Checked is false, then your lubeJob_var has never been given an initial value. So you can't use it.

你需要先给total赋值。

    decimal total=0;

    decimal total = number1 + number2;

这个例子可能对你有帮助。

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            decimal number1 = 53m;

        decimal number2 = 33m;

        decimal total; 

        total = number1 + number2; 

        Console.WriteLine(total);
    }
}
}

http://rextester.com/CTJQ60331