我试图通过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"));
}
}