首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

步骤的返回值

2014-01-06 
方法的返回值public bool asd(){int count 0for (int i 1 i 9i++ ){if (true){if (count 0){i

方法的返回值

public bool asd()
        {
            int count = 0;
            for (int i = 1; i < 9;i++ )
            {
                if (true)
                {
                    if (count == 0)
                    {
                        if (true)
                        {
                            return true;
                        }
                        else
                        {
                            return true;
                        }
                    }

                }
                else
                {
                    if (i > 3)
                    {
                        if (true)
                        {
                            i = 2;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (true)
                        { }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
        }



求教 怎么会并非所有的代码路径都有返回值?


[解决办法]

引用:
public bool asd()
        {
            int count = 0;
            for (int i = 1; i < 9; i++)
            {
                if (count == 0)
                {
                    return true;
                }
                else
                {
                    if (i > 3)
                    {
                        if (count == 0)
                        {
                            i = 2;
                            return true;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (count == 0)
                        {
                               return true;
                         }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
        }
这个你试了么 ? 不对的


嗯,在你for语句块后面加个return true就行了,刚才没注意
public bool asd()
        {


            int count = 0;
            for (int i = 1; i < 9; i++)
            {
                if (count == 0)
                {
                    return true;
                }
                else
                {
                    if (i > 3)
                    {
                        if (count == 0)
                        {
                            i = 2;
                            return true;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (count == 0)
                        {
                               return true;
                         }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            return true;       --这里要加个
        }


[解决办法]
for外面加一个返回,就解决了

[解决办法]
你这个程序,直接写 return true 就完了。

人要动脑筋,不要仅仅为了抄别人的代码形式才编程。

首先,看到你的第一个 if (true) 代码,那么你就可以把 else {} 部分删除掉了。而且剩下部分的if语句也不需要了。

然后,看到 if(count==0)内部的 if(true) 也是一样。最后就剩下了
public bool asd()
        {
            int count = 0;
            for (int i = 1; i < 9;i++ )
            {
                    if (count == 0)
                    {


                            return true;
                    }
            }
         }


最后你看到,for循环也是多余的,根本没有改变count==0的初始值,因此立刻return,所以你这个程序就剩下了 return true 这一句就行了。
[解决办法]

热点排行