[英]Why do I get an error when I try to build?
Trying to build and then run my first program is giving the below error. 尝试构建然后运行我的第一个程序时,出现以下错误。
Book.cs(50,27): error CS0161: 'Book.GetStatistics()': not all code paths return a value [C:\\dev\\gradebook\\src\\GradeBook\\GradeBook.csproj] Book.cs(50,27):错误CS0161:“ Book.GetStatistics()”:并非所有代码路径都返回值[C:\\ dev \\ gradebook \\ src \\ GradeBook \\ GradeBook.csproj]
Line 50 is: public Statistics GetStatistics() 第50行是:public Statistics GetStatistics()
Any help would be amazing! 任何帮助都将是惊人的!
Tried to rebuild and saved all changes 尝试重建并保存所有更改
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;
}
}
The return result;
return result;
statement is inside the for
loop. 语句在
for
循环内。 This means that if there are no grades, or if all grades are 42.1, the return statement will not be reached. 这意味着,如果没有等级,或者所有等级均为42.1,将不会返回return语句。
You probably meant to place the return statement below the closing }
of the for-loop (most of the average computation should probably be moved there, too). 您可能打算将return语句放在for循环的结束
}
下(大多数平均计算也可能也应移到该处)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.