在哪里声明变量
本文关键字:变量 声明 在哪里 | 更新日期: 2023-09-27 18:29:29
我正在尝试生成一个平均函数。Total
将这些值相加,然后Total
除以n
,即条目数。
无论我把double Total;
放在哪里,我都会收到一条错误消息。在这个例子中,我得到了一个例外:
使用未分配的局部变量"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;
}
}
}
}
要解决问题,请给Total
一个默认值:
double Total = 0;
此外,您并没有添加到总数中。因此,将您的=
更改为+=
:
Total += argMachineDataList[n];
这里的问题是,不能保证参数(argMachineList
)传递的项将具有大于或等于1的Count
属性(这是循环至少迭代一次所必需的)。如果有人将new List<double>()
传递到此函数中,它将运行并编译,但由于Count
属性为0,因此永远不会进入for循环。因此,for循环完全有可能永远不会迭代一次,Total
的值永远不会设置,因此您将无法正确返回,因为您的返回语句基本上是"指向双精度/某个整数n的指针"。请注意,如果将Total = argMachineList[1]
(例如)拉到for循环之外,编译器错误就会消失。
每个人将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;
}
只要这样做,你就会没事的:
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]之间添加了一个+=
,因为因为您想要总和,所以您需要实际合计这些值。
您需要初始化Total:
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
您需要初始化总
double total = 0.0;
通过在for循环中设置n=1,您还可以从第二个元素而不是第一个元素开始。
您的代码中有几个错误,其中一个尚未提及的错误是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
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;
}
您的循环只能运行一次,使Total
处于未锁定状态。申报时的Init Total。
double Total = 0.0;
这应该可以解决问题。。。
double Total = 0;
问题是,你要做的是使用一个尚未赋值的变量。在使用变量之前,必须为其赋值。
最初的double Total;
是声明变量,而不是分配。