错误“;avg1、avg2、avg3、avg4”;不能在此范围内声明局部变量

本文关键字:范围内 声明 局部变量 不能 avg4 avg1 avg2 avg3 错误 | 更新日期: 2024-10-23 09:09:35

我正在尝试传递outputdisp方法上的所有avg1、avg2、avg3和avg4。我不知道是怎么回事,因为同时在第二个方法avrg上,我也从另一个方法收到了,但我也需要给出它。

 static void Main(string[] args)
 {
 {
 double totalspendf = 0, totalspendc = 0, totalspendcr = 0, totalspendo = 0, avg1=0, avg2=0, avg3=0, avg4=0;
 dailyspend(ref totalspendf, ref totalspendc, ref totalspendcr, ref totalspendo);
 avrg(totalspendf, totalspendc, totalspendcr, totalspendo, avg1, avg2, avg3, avg4);
 outputdisp(totalspendc, totalspendf, totalspendcr, totalspendo, avg1, avg2, avg3, avg4);
 Console.ReadLine();
 }
 }
 static void dailyspend(ref double totalspendf, ref double totalspendc, ref double totalspendcr, ref double totalspendo)
 { int days;
 double spendf = 0, spendc = 0, spendcr = 0, spendo = 0;
 for (days = 1; days <= 2; days++)
 {
 Console.WriteLine("");
 do
 {
 Console.Write("Food : ");
 spendf = double.Parse(Console.ReadLine());
 } while (spendf < 0);
 do
 {
 Console.Write("Clothing : ");
 spendc = double.Parse(Console.ReadLine());
 } while (spendc < 0);
 do
 {
 Console.Write("College related : ");
 spendcr = double.Parse(Console.ReadLine());
 } while (spendcr < 0);
 do
 {
 Console.Write("Outside: ");
 spendo = double.Parse(Console.ReadLine());
 } while (spendo < 0);
 Console.WriteLine("");
 totalspendf += spendf;
 totalspendc += spendc;
 totalspendcr += spendcr;
 totalspendo += spendo;
 }
 }
 public static double avrg(double totalspendf, double totalspendc, double totalspendcr, double totalspendo, double avg1, double avg2, double avg3, double avg4)
 {
 double avg, avg1, avg2, avg3, avg4;
 avg1 = totalspendc / 2;
 avg2 = totalspendcr / 2;
 avg3 = totalspendf / 2;
 avg4 = totalspendo / 2;
 avg = (totalspendc + totalspendcr + totalspendf + totalspendo) / 2;
 return avg;
}
static void outputdisp(double totalspendc, double totalspendf,double totalspendcr,double totalspendo,double avg1, double avg2, double avg3,double avg4)
{ double avg;
Console.WriteLine("Categories               Total            Average    ");
Console.WriteLine("Clothes                  " + totalspendc.ToString("C") + "           " + avg1.ToString("C"));
Console.WriteLine("Food                     " + totalspendf.ToString("C") + "           " + avg3.ToString("C"));
Console.WriteLine("College related          " + totalspendcr.ToString("C")+ "           " + avg2.ToString("C"));
Console.WriteLine("Outside                  " + totalspendo.ToString("C") + "           " + avg4.ToString("C"));
Console.WriteLine("");
avg = avrg(totalspendf, totalspendc, totalspendcr, totalspendo);
Console.WriteLine("Average spend for this week : " + avg.ToString("C"));
}
}

错误“;avg1、avg2、avg3、avg4”;不能在此范围内声明局部变量

您已经在函数中声明了avg、avg1、avg2、avg3、avg4,这就是它抛出错误的原因。删除以下语句或更改变量名

 double avg, avg1, avg2, avg3, avg4;

您的问题将得到解决

函数"avrg"已经具有相同名称的参数,即

avg1、avg2、avg3、avg4。

要么删除这些参数,要么更改变量的名称。

我已经更新了你的代码。希望这将为你工作-

    static void Main(string[] args)
    {
        double totalspendf = 0, totalspendc = 0, totalspendcr = 0, totalspendo = 0, avg1 = 0, avg2 = 0, avg3 = 0, avg4 = 0;
        dailyspend(ref totalspendf, ref totalspendc, ref totalspendcr, ref totalspendo);
        avrg(totalspendf, totalspendc, totalspendcr, totalspendo, ref avg1, ref avg2, ref avg3, ref avg4);
        outputdisp(totalspendc, totalspendf, totalspendcr, totalspendo, avg1, avg2, avg3, avg4);
        Console.ReadLine();
    }
    static void dailyspend(ref double totalspendf, ref double totalspendc, ref double totalspendcr, ref double totalspendo)
    {
        int days;
        double spendf = 0, spendc = 0, spendcr = 0, spendo = 0;
        for (days = 1; days <= 2; days++)
        {
            Console.WriteLine("");
            do
            {
                Console.Write("Food : ");
                spendf = double.Parse(Console.ReadLine());
            } while (spendf < 0);
            do
            {
                Console.Write("Clothing : ");
                spendc = double.Parse(Console.ReadLine());
            } while (spendc < 0);
            do
            {
                Console.Write("College related : ");
                spendcr = double.Parse(Console.ReadLine());
            } while (spendcr < 0);
            do
            {
                Console.Write("Outside: ");
                spendo = double.Parse(Console.ReadLine());
            } while (spendo < 0);
            Console.WriteLine("");
            totalspendf += spendf;
            totalspendc += spendc;
            totalspendcr += spendcr;
            totalspendo += spendo;
        }
    }
    public static double avrg(double totalspendf, double totalspendc, double totalspendcr, double totalspendo, ref double avg1,ref double avg2,ref double avg3,ref double avg4)
    {
        double avg;
        avg1 = totalspendc / 2;
        avg2 = totalspendcr / 2;
        avg3 = totalspendf / 2;
        avg4 = totalspendo / 2;
        avg = (totalspendc + totalspendcr + totalspendf + totalspendo) / 2;
        return avg;
    }