简体   繁体   English

从内部循环返回值不显示

[英]return value from within loop not showing

Morning, simple stupid question. 早上,简单愚蠢的问题。 I have found post with similar issues but after reading through it does not solve my error. 我发现有类似问题的帖子,但通读后无法解决我的错误。

Return value from For loop 从For循环返回值

Can't get a return value from a foreach loop inside a method 无法从方法内的foreach循环中获取返回值

The methods: meth1 meth2 ect....all return a value but at the moment I am getting the error 方法:meth1 meth2 ect ....都返回一个值,但此刻我遇到了错误

"Error 1 'Proj5.Program.meth1(int)': not all code paths return a value" for each meth method. 对于每个meth方法,“错误1'Proj5.Program.meth1(int)':并非所有代码路径都返回一个值”。

My logical guess is its not seeing the value within the loop?? 我的逻辑猜测是它没有看到循环中的值? ... ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Proj5
{
class Program
{
    static void Main()
    {
        for (int i = 1; i < 101; i++)
        {
            if (i == 3 || 0 == (i % 3) || 0 == (i % 5) || i == 5)
            {
                Console.Write(meth1(i));
                Console.Write(meth2(i));
                Console.Write(meth3(i));
                Console.Write(meth4(i));
            }
            else
            {
                Console.Write(TheInt(i));
            }
        }
        Console.ReadLine();
    }

    static string meth1(int i)
    {
        string f = "fuzz";

        if (i == 3)
        {
            return f;
        }
    }
    static string meth2(int i)
    {
        string f = "fuzz";

        if (0 == (i % 3))
        {
            return f;
        }
    }
    static string meth3(int i)
    {
        string b = "buzz";

        if (i == 5)
        {
            return b;
        }

    }
    static string meth4(int i)
    {
        string b = "buzz";

        if (0 == (i % 5))
        {
            return b;
        }
    }
    static int TheInt(int i)
    {
        return i;
    }

}
}

You say your method should return a string, but if i<>3, you don't say what should be returned. 您说您的方法应该返回一个字符串,但是如果i <> 3,则不说应该返回什么。 Method 2 and 3 have the same problem, by the way (and 4 also). 顺便说一下,方法2和3也有同样的问题(也有4)。 I won't speak about method TheInt, which is... funny ;) 我不会谈论方法TheInt,这很有趣。

Correction 更正

static string meth1(int i)
    {
        string f = "fuzz";

        if (i == 3)
        {
            return f;
        }
        return null;//add a "default" return, null or string.Empty
    }

or shorter 或更短

static string meth1(int i) {
    return (i == 3) ? "fuzz" : null/*or string.Empty*/;
}

Your functions only return when the if is evaluated to true. 仅当if评估为true时,函数才返回。 Add return statement outside the if also, or add else statement, and your code will compile and work. 在if also之外添加return语句,或添加else语句,您的代码将编译并运行。

static string meth2(int i)
{
    string f = "fuzz";

    if (0 == (i % 3))
    {
        return f;
    }
    else
      return "";
}

When you declare a method as returning a values (as for meth1, etc) you should honor this declaration. 当您将方法声明为返回值时(例如meth1等),您应该遵守该声明。

Your method doesn't return anything if the internal condition is not met. 如果内部条件不满足,您的方法将不会返回任何内容。 The compiler notices this and complains with you 编译器注意到这一点并向您抱怨

You should ensure that every possible path of execution returns something out of the called method to the caller. 您应确保所有可能的执行路径都将被调用方法中的某些内容返回给调用方。

For example 例如

static string meth3(int i)     
{         
    string b = "buzz";          
    if (i == 5)         
    {             
        return b;         
    }      
    // What if the code reaches this point?
    // What do you return to the caller?
    return string.Empty; // or whatever you decide as correct default for this method
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM