public static string 五行判断(string 数字)
        {
            if (数字.EndsWith("1"))
            {
                return "水";
            }
            if (数字.EndsWith("6"))
            {
                return "水";
            }
            if (数字.EndsWith("2"))
            {
                return "火";
            }
            if (数字.EndsWith("7"))
            {
                return "火";
            }
            if (数字.EndsWith("3"))
            {
                return "木";
            }
            if (数字.EndsWith("8"))
            {
                return "木";
            }
            if (数字.EndsWith("4"))
            {
                return "金";
            }
            if (数字.EndsWith("9"))
            {
                return "金";
            }
            if (数字.EndsWith("0"))
            {
                return "土";
            }
            if (数字.EndsWith("5"))
            {
                return "土";
            }
        }错误 1 “逻辑关系.BLL双色球数据.五行判断(string)”: 并非所有的代码路径都返回值
这个是什么毛病呢?怎么修改呀

解决方案 »

  1.   

    比较简单的,可以在“五行判断”方法的最后加上个return string.Empty;
      

  2.   

    最后加上return null,你在外面就可以判断返回值是不是等于null了
      

  3.   

    另外根据你的代码逻辑,考虑到效率,我觉得用switch... case...代码块更合适:
    public static string 五行判断(string 数字)
    {
    if (string.IsNullOrEmpty(数字))
    throw new ArgumentException("参数“数字”不合法,不能为null或空串!");
    char lastChar = 数字[数字.Length - 1];
    switch (lastChar)
    {
    case '1':
    case '6': return "水";
    case '2':
    case '7': return "火";
    //其他的case
    default: return string.Empty;
    }
    }
      

  4.   

    你这里有10个if语句,都是在if语句里面return的。
    如果这10个if条件都不成立的话,那么你就没有返回值了。
    所以得在最后加上一句:return string.Empty;或return null;
      

  5.   

    改成这样:public static string 五行判断(string 数字)
            {
                if (数字.EndsWith("1"))
                {
                    return "水";
                }
                if (数字.EndsWith("6"))
                {
                    return "水";
                }
                if (数字.EndsWith("2"))
                {
                    return "火";
                }
                if (数字.EndsWith("7"))
                {
                    return "火";
                }
                if (数字.EndsWith("3"))
                {
                    return "木";
                }
                if (数字.EndsWith("8"))
                {
                    return "木";
                }
                if (数字.EndsWith("4"))
                {
                    return "金";
                }
                if (数字.EndsWith("9"))
                {
                    return "金";
                }
                if (数字.EndsWith("0"))
                {
                    return "土";
                }
                if (数字.EndsWith("5"))
                {
                    return "土";
                }
                
                return "";
            }