简单错误 "Not all code paths return a value" 和 "use of unassigned local variable"
Simple errors, "Not all code paths return a value", and "use of unassigned local variable"
我正在编写的这个程序可以在学生输入他们的成绩和学分后计算每门课程的 GPA 和质量分数。
我遇到问题的部分是计算每门课程的质量分数。我将字母等级和学分转储到 2 个不同的数组中,我创建了第三个名为 QualityPts 的数组,它将用于存储每个 class.
的总质量分数
这将通过使用索引位置来计算以确定将在其他 2 个数组中使用的两个值。现在我用另一种方法来做这个,我收到一条错误消息
"Not all code paths return a value".
第二个错误与我的新变量 "QualityPts" 有关,它只在这个新方法中。它说 "Use of unassigned local variable"。
这 2 个错误都在方法 CalcQP()
中。
我的代码如下:
private decimal[] grades;
private decimal[] Credits;
private decimal CalcQP()
{
decimal[] QualityPts;
string msg="The total quality Points for this course is: ";
for (int i = 0; i < grades.Length; i++)
{
QualityPts[i] = grades[i] * Credits[i];
lbQuality.Items.Add(msg + QualityPts[i]);
}
}
看起来 CalcQP()
return 类型应该是无效的。该方法的目的是否只是将项目添加到 lbQuality
?我不确定从代码片段中 return 还应该是什么。
基本上编译器正在停止,因为它可以看到该方法应该 returning 一个双精度但不包含 return 语句。
第二个错误是为您保存运行时错误,因为编译器可以看到您正在尝试使用尚未初始化的变量。所以你只需要初始化 QualityPts
:
decimal[] QualityPts = new decimal[grades.Length];
private decimal calcQP()
方法应该 return 十进制值或将 decimal
更改为 void
.
此外,QualityPts
应该被初始化:decimal[] QualityPts = new decimal[grades.Length];
Not all code paths return a value
您已声明您的方法需要 return 类型的小数
private decimal CalcQP()
但你从来没有 return 小数点。
您需要 return 一个 (return QualtityPts[i]
)
Return数组
private decimal[] CalcQP()
... return QualityPts;
或者什么都不return
private void CalcQP()
Use of unassigned local variable
您正在尝试使用 QualityPts
,但您尚未声明它,因此 [i]
将没有任何价值(或尚不存在)
decimal[] QualityPts = new decimal[grades.Length];
您正在声明 QualityPts
但未对其进行初始化。这就是它给出错误的原因。遵循标准做法,即在声明变量时始终初始化变量。像下面那样做:
decimal QualityPts = new decimal[length]; // Replace length with your length.
第二件事 CalcQP()
方法 return 类型是十进制,而您没有 returning 任何东西。您必须从中得到 return 十进制值。要么使该方法无效。像下面那样做:
public void CalcQP()
如果您有其他问题,请告诉我。
我正在编写的这个程序可以在学生输入他们的成绩和学分后计算每门课程的 GPA 和质量分数。
我遇到问题的部分是计算每门课程的质量分数。我将字母等级和学分转储到 2 个不同的数组中,我创建了第三个名为 QualityPts 的数组,它将用于存储每个 class.
的总质量分数这将通过使用索引位置来计算以确定将在其他 2 个数组中使用的两个值。现在我用另一种方法来做这个,我收到一条错误消息
"Not all code paths return a value".
第二个错误与我的新变量 "QualityPts" 有关,它只在这个新方法中。它说 "Use of unassigned local variable"。
这 2 个错误都在方法 CalcQP()
中。
我的代码如下:
private decimal[] grades;
private decimal[] Credits;
private decimal CalcQP()
{
decimal[] QualityPts;
string msg="The total quality Points for this course is: ";
for (int i = 0; i < grades.Length; i++)
{
QualityPts[i] = grades[i] * Credits[i];
lbQuality.Items.Add(msg + QualityPts[i]);
}
}
看起来 CalcQP()
return 类型应该是无效的。该方法的目的是否只是将项目添加到 lbQuality
?我不确定从代码片段中 return 还应该是什么。
基本上编译器正在停止,因为它可以看到该方法应该 returning 一个双精度但不包含 return 语句。
第二个错误是为您保存运行时错误,因为编译器可以看到您正在尝试使用尚未初始化的变量。所以你只需要初始化 QualityPts
:
decimal[] QualityPts = new decimal[grades.Length];
private decimal calcQP()
方法应该 return 十进制值或将 decimal
更改为 void
.
此外,QualityPts
应该被初始化:decimal[] QualityPts = new decimal[grades.Length];
Not all code paths return a value
您已声明您的方法需要 return 类型的小数
private decimal CalcQP()
但你从来没有 return 小数点。
您需要 return 一个 (return QualtityPts[i]
)
Return数组
private decimal[] CalcQP()
... return QualityPts;
或者什么都不return
private void CalcQP()
Use of unassigned local variable
您正在尝试使用 QualityPts
,但您尚未声明它,因此 [i]
将没有任何价值(或尚不存在)
decimal[] QualityPts = new decimal[grades.Length];
您正在声明 QualityPts
但未对其进行初始化。这就是它给出错误的原因。遵循标准做法,即在声明变量时始终初始化变量。像下面那样做:
decimal QualityPts = new decimal[length]; // Replace length with your length.
第二件事 CalcQP()
方法 return 类型是十进制,而您没有 returning 任何东西。您必须从中得到 return 十进制值。要么使该方法无效。像下面那样做:
public void CalcQP()
如果您有其他问题,请告诉我。