尝试构建然后运行我的第一个程序时,出现以下错误。
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;
}
}
答案 0 :(得分:0)
return result;
语句位于for
循环的内部中。这意味着,如果没有等级,或者所有等级均为42.1,将不会返回return语句。
您可能打算将return语句放在for循环的结束}
下方(大多数平均计算也可能也应该移到那里)。