我正在尝试制作一个普通的功能。 Total
添加了值,然后Total
除以n
条目数。
无论我把double Total;
放在哪里,都会收到错误消息。在这个例子中,我得到了例外:
Use of unassigned local variable 'Total'
如果我将它放在Average
方法之前,则两个引用都会显示为错误。我确定这很简单...
namespace frmAssignment3
{
class StatisticalFunctions
{
public static class Statistics
{
public static double Average(List<double> argMachineDataList)
{
double Total;
int n;
for (n = 1; n <= argMachineDataList.Count; n++) {
Total = argMachineDataList[n];
}
return Total / n;
}
public static double StDevSample(
List<MachineData.MachineRecord> argMachineDataList)
{
return -1;
}
}
}
}
答案 0 :(得分:5)
要解决您的问题,请为Total
提供默认值:
double Total = 0;
另外,你不是要加总。因此,将=
更改为+=
:
Total += argMachineDataList[n];
答案 1 :(得分:3)
这里的问题是无法保证参数(argMachineList
)传递的项目具有大于或等于1的Count
属性(这对于循环是必需的)至少迭代一次)。如果有人将new List<double>()
传递给此函数,它将运行并编译,但for循环将永远不会进入,因为Count
属性为0.所以完全有可能for循环永远不会迭代一次,Total
的值永远不会被设置,因此你将无法正确返回,因为你的return语句基本上是“指向double / some integer n”。请注意,如果要在for循环之外拉Total = argMachineList[1]
(例如),编译器错误就会消失。
每个人将Total
的默认值设置为0的简单建议消除了问题; Total
将始终能够返回一个值,因为无论for循环是否迭代一次,它的值都将被设置。
public static double Average(List<double> argMachineList)
{
double Total = 0.0;
int n;
for (n = 0; n < argMachineList.Count; n++)
{
Total += argMachineList[n];
}
return Total / argMachineList.Count;
}
答案 2 :(得分:1)
只要这样做就可以了:
public static double Average(List<double> argMachineDataList)
{
double Total = 0.0;
int n;
for (n = 0; n < argMachineDataList.Count; n++)
{
Total += argMachineDataList[n]; // added += since you probably want to sum value
}
return Total / n;
}
我还在Total和你的argMachineDataList [n]之间添加了一个+=
,因为你需要实际总结这些值所需的总和。
答案 3 :(得分:1)
您需要初始化总计:
double Total = 0;
或如 @Kirk Woll 使用 linq :
double Total = argMachineDataList.Average();
避免写:
for (int n = 0; n < argMachineDataList.Count; n++)
//the first index of the list start from 0 and the last one is n-1 so
//argMachineDataList.Count-1 so you need < not <=
{
Total += argMachineDataList[n];
//Remember that if you want to
//increment total you need the += instead of =
}
return Total / argMachineDataList.Count;
//Then remeber that n int this case is a local variable so you can't use it out
//of the for loop
答案 4 :(得分:0)
你需要初始化总数
double total = 0.0;
你也是通过在for循环中设置n = 1来开始使用第二个元素而不是第一个元素。
答案 5 :(得分:0)
这应解决问题......
double Total = 0;
问题是,您正在尝试做的是使用尚未赋值的变量。在使用变量之前,必须为其赋值。
您最初拥有的内容,double Total;
声明变量,而不是分配。
答案 6 :(得分:0)
你的循环可能只运行一次,Total
未被激活。声明它时的Init Total。
double Total = 0.0;
答案 7 :(得分:0)
您的代码中有几个错误,一个尚未提及的错误是for
循环中的“一个一个”错误 - 您要添加的第一个元素的索引为1,但是集合中的第一项具有零索引:
double total = 0;
for (int n = 0; n < argMachineDataList.Count; n++)
{
total += argMachineDataList[n];
}
return total / argMachineDataList.Count; // will throw exception if n = 0
答案 8 :(得分:0)
public static double Average(List<double> argMachineDataList)
{
//First lets handle the case where argMachineDataList is empty.
// Otherwise we'll return Double.NaN when we divide by zero.
if(argMachineDataList.Count == 0)
{
throw new ArgumentException("argMachineDataList cannot be an empty List.");
}
//Second we have to assign an initial value to Total,
// because there is no guarantee it will be set in the loop.
//And since it's a local variable, it will not automatically be set.
double Total = 0.0;
//Next since Lists are Zero Base indexed,
// we'll want to start with n = 0 so we include the first element.
//We also want the loop to stop before n = argMachineDataList.Count,
// or we'll get an ArgumentOutOfRange Exception when trying to access
// argMachineDataList[n] (when n = argMachineDataList.Count).
for (int n = 0; n < argMachineDataList.Count; n++)
{
//Since we want to add the value of argMachineDataList[n] to Total,
// we have to change from = to +=
// (which is the same as saying Total = Total + argMachineDataList[n]).
Total += argMachineDataList[n];
}
//Lastly, n will be out of scope outside of the for loop.
// So we'll use argMachineDataList.Count, to get the number of items.
return Total / argMachineDataList.Count;
}