并非所有代码路径都返回一个值 - 枚举练习

时间:2017-09-22 09:42:34

标签: c# enums return

我尝试执行一个简单的代码来研究枚举主题。 然而,我遇到了这个问题:“并非所有代码路径都返回一个值”。 这是代码:

namespace ConsoleAppTest
{
    class Program
    {
        enum Seasons { Winter, Spring, Summer, Fall };

        static void Main(string[] args)
        {
            WhichSeason(3);
        }

        static Seasons WhichSeason(int month)
        {
            if (month >= 1 || month <= 3)
            {
                return Seasons.Winter;
            }
            else if (month >= 4 || month <= 6)
            {
                return Seasons.Spring;
            }
            else if (month >= 7 || month <= 9)
            {
                return Seasons.Summer;
            }
            else if (month >= 10 || month <= 12)
            {
                return Seasons.Fall;
            }
        }
    }
}

我想知道什么可能导致这个问题。 谢谢:))

5 个答案:

答案 0 :(得分:5)

你应该处理else案件。您的month整数也可以是<1>12

static Seasons WhichSeason(int month)
{
    if (month >= 1 && month <= 3)
    {
        return Seasons.Winter;
    }
    else if (month >= 4 && month <= 6)
    {
        return Seasons.Spring;
    }
    else if (month >= 7 && month <= 9)
    {
        return Seasons.Summer;
    }
    else if (month >= 10 && month <= 12)
    {
        return Seasons.Fall;
    }
    else
    {
        throw new ArgumentOutOfRangeException("invalid month");
    }
}

所以如果你打电话

WhichSeason(13); //throws exception

答案 1 :(得分:2)

如果month,说-1123,应该返回什么?您可以通过两种主要方式解决问题, silent

    // Please, notice "None"
    enum Seasons { None, Winter, Spring, Summer, Fall };

    static void Main(string[] args)
    {
        WhichSeason(3);
    }

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 3)
            return Seasons.Winter;
        else if (month >= 4 && month <= 6)
            return Seasons.Spring;
        else if (month >= 7 && month <= 9)
            return Seasons.Summer;
        else if (month >= 10 && month <= 12)
            return Seasons.Fall;
        else
            return Seasons.None;
    }

或者在案例

中抛出适当的例外ArgumentOutOfRangeException
    enum Seasons {  Winter, Spring, Summer, Fall };

    static void Main(string[] args)
    {
        WhichSeason(3);
    }

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 3)
            return Seasons.Winter;
        else if (month >= 4 && month <= 6)
            return Seasons.Spring;
        else if (month >= 7 && month <= 9)
            return Seasons.Summer;
        else if (month >= 10 && month <= 12)
            return Seasons.Fall;
        else
            throw new ArgumentOutOfRangeException(
              "month", 
              "month must be in [1..12] range."); // Exception
    }

修改:我保留了WhichSeason 完整,但似乎您在实施中遇到了逻辑错误正确的例程应该是

    static Seasons WhichSeason(int month)
    {
        if (month >= 1 && month <= 2 || month == 12) // Jan, Feb and Dec 
            return Seasons.Winter;
        else if (month >= 3 && month <= 5) // Mar-May
            return Seasons.Spring;
        else if (month >= 6 && month <= 8) // Jun-Aug
            return Seasons.Summer;
        else if (month >= 9 && month <= 11) // Sep-Nov
            return Seasons.Fall;
        else
            return Seasons.None;
    }

答案 2 :(得分:2)

你应该添加&amp;&amp;逻辑中的运算符,如果输入在描述的条件中不匹配,也应处理 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{

     class Program
    {
        enum Seasons { Winter, Spring, Summer, Fall,NotAValidInput };

       public static void Main(string[] args)
        {
           Console.WriteLine(WhichSeason(-1));
        }

        static Seasons WhichSeason(int month)
        {
            if (month >= 1 && month <= 3)
            {
                return Seasons.Winter;
            }
            else if (month >= 4 && month <= 6)
            {
                return Seasons.Spring;
            }
            else if (month >= 7 && month <= 9)
            {
                return Seasons.Summer;
            }
            else if (month >= 10 && month <= 12)
            {
                return Seasons.Fall;
            }
            return Seasons.NotAValidInput;
        }
    }

}

答案 3 :(得分:0)

在else下面引入一个else块。方法不是在所有情况下都返回值,应该使用else备份。

答案 4 :(得分:0)

带开关

static Seasons WhichSeason(int month)
{
    switch (month)
    {           
        case 1:
        case 2:
        case 3:
            return Seasons.Spring;
        case 4:
        case 5:
        case 6:
            return Seasons.Summer;
        case 7:
        case 8:
        case 9:
            return Seasons.Fall;
        case 10:
        case 11:
        case 12:
            return Seasons.Winter;
        default:
            throw new Exception("The month is invalid!");
    }
}