尝试构建然后运行我的第一个程序会给出以下错误。
Book.cs(50,27(: 错误 CS0161: 'Book.GetStatistics((': 并非所有代码路径都返回值 [C:\dev\gradebook\src\GradeBook\GradeBook.csproj]
第 50 行是:public Statistics GetStatistics((
任何帮助都会很棒!
尝试重建并保存所有更改
using System;
using System.Collections.Generic;
namespace GradeBook
{
public class Book
{
public Book(string name)
{
grades = new List<double>();
Name = name;
}
public void AddLetterGrade(char letter)
{
switch(letter)
{
case 'A':
AddGrade(90);
break;
case 'B':
AddGrade(80);
break;
case 'C':
AddGrade(70);
break;
default:
AddGrade(0);
break;
}
}
public void AddGrade(double grade)
{
if (grade <= 100 && grade >= 0)
{
grades.Add(grade);
}
else
{
Console.WriteLine("Invalid Value");
}
}
public Statistics GetStatistics()
{
var result = new Statistics();
result.Average = 0.0;
result.High = double.MinValue;
result.Low = double.MaxValue;
for (var index =0; index < grades.Count; index++)
{
if (grades[index] == 42.1)
{
continue;
}
result.Low = Math.Min(grades[index], result.Low);
result.High = Math.Max(grades[index], result.High);
result.Average += grades[index];
result.Average /= grades.Count;
switch(result.Average)
{
case var d when d >= 90.0:
result.Letter = 'A';
break;
case var d when d >= 80.0:
result.Letter = 'B';
break;
case var d when d >= 70.0:
result.Letter = 'C';
break;
case var d when d >= 60.0:
result.Letter = 'D';
break;
default:
result.Letter = 'F';
break;
}
return result;
}
}
private List<double> grades;
public string Name;
}
}
return result;
语句位于for
循环内。这意味着,如果没有成绩,或者所有成绩均为 42.1,则不会达到返回语句。
您可能打算将 return 语句放在 for 循环的闭}
下方(大多数平均计算可能也应该移到那里(。