采用params关键字声明不确定个参数,然后求出平均值,得到的结果总是{0},不知道哪里出错了,请各位指点一下。
using system;
class Method
{
public static float Varlenparam(params long[] v)
{
long total, i;
for (i = 0, total = 0; i < v.Length; ++i)
total += v[i];
return (float)total / v.Length;
}
static void Main()
{
float x = Varlenparam(1, 2, 3, 5);
Console.WriteLine("1+2+3+5的平均值是{0}", x);
x = Varlenparam(4, 5, 6);
Console.WriteLine("4+5+6的平均值是{0}", x);
Console.ReadKey();
}
}以下是调试结果:
1+2+3+5的平均值是{0}
4+5+6的平均值是{0}
using system;
class Method
{
public static float Varlenparam(params long[] v)
{
long total, i;
for (i = 0, total = 0; i < v.Length; ++i)
total += v[i];
return (float)total / v.Length;
}
static void Main()
{
float x = Varlenparam(1, 2, 3, 5);
Console.WriteLine("1+2+3+5的平均值是{0}", x);
x = Varlenparam(4, 5, 6);
Console.WriteLine("4+5+6的平均值是{0}", x);
Console.ReadKey();
}
}以下是调试结果:
1+2+3+5的平均值是{0}
4+5+6的平均值是{0}
//再度提醒>>>>>注意细节!!!
float x = Varlenparam(1, 2, 3, 5);
Console.WriteLine("1+2+3+5的平均值是{0}", x);//你上面的花括号不是英文字符
x = Varlenparam(4, 5, 6);
Console.WriteLine("4+5+6的平均值是{0}", x);
Console.ReadKey();
{
long total, i;
for (i = 0, total = 0; i < v.Length; ++i)
total += v[i];
return (float)total / v.Length;
}
static void Main()
{
float x = Varlenparam(1, 2, 3, 5);
Console.WriteLine("1+2+3+5的平均值是{0}", x);
x = Varlenparam(4, 5, 6);
Console.WriteLine("4+5+6的平均值是{0}", x);
Console.ReadKey();
}
class Method
{
public static float Varlenparam(params long[] v)
{
long total=0, i;
for (i = 0; i < v.Length; ++i)
total += v[i];
return (float)total / v.Length;
}
static void Main()
{
float x = Varlenparam(1, 2, 3, 5);
Console.WriteLine("1+2+3+5的平均值是{0}", x);
x = Varlenparam(4, 5, 6);
Console.WriteLine("4+5+6的平均值是{0}", x);
Console.ReadKey();
}
}total 赋值的地方不对..因为那样
{0}居然用中文花括号,晕