简体   繁体   English

为什么在尝试构建时出现错误?

[英]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.

相关问题 我尝试这样做时出现模板错误? - Get a template error when I try to do this? 当我尝试运行或启动我的 win 服务时,为什么会出现错误? - Why do i get Error when i try to run or start my win service? 当我尝试调用我的方法时,为什么会出现对象引用错误? - Why do I get an object reference error when i try to call my method? 当我尝试通过Entity Framework中的路径/文件名打开MDF数据库时,为什么会出现此错误? - Why do I get this error when I try to open an MDF database via path/filename in Entity Framework? 当我尝试在我的处理程序中调用应用程序变量时,为什么会出现此错误? - why do I get this error when I try to call an application variable in my handler? 当我尝试从控制台应用程序访问EF时,为什么会收到与app.config相关的错误? - Why do i get this app.config related error when i try to acess EF from console application? 为什么在尝试初始化Interop.Word.Application时得到COMException? - Why do I get a COMException when I try to initialize an Interop.Word.Application? 当我第二次尝试将流转换为图像时,为什么会出现 ArgumentException? C# - Why do I get ArgumentException when I try to convert stream to Image for second time? C# 尝试执行托管代码时,为什么会得到PInvoceStackImbalance - Why do I get PInvoceStackImbalance when I try to execute umanaged code 当我尝试使用Web API 2的属性路由时,为什么会出现InvalidOperationException? - Why do I get an InvalidOperationException when I try to use attribute routing with Web API 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM