我正试图通过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"));
}
}
答案 0 :(得分:0)
功能&#34; avrg&#34;已经有相同名称的参数,即
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;
}
答案 1 :(得分:0)
你已经在函数中声明了avg,avg1,avg2,avg3,avg4,这就是它抛出错误的原因。 删除以下语句或更改变量名称
double avg, avg1, avg2, avg3, avg4;
你的问题将得到解决