2016-04-21 43 views
0

我试图通过outputdisp方法上的所有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, 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")); 

} 
} 

回答

0

功能 “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; 

    } 
0

你已经宣布AVG,AVG1,AVG2,avg3,avg4的功能,这就是为什么它是抛出错误。 删除下面的语句或改变变量名

double avg, avg1, avg2, avg3, avg4; 

您的问题将得到解决

相关问题