【问题标题】:not all code paths return a value, and need to get 2 variables out of a method并非所有代码路径都返回一个值,并且需要从方法中获取 2 个变量
【发布时间】:2015-12-23 13:59:34
【问题描述】:

我试图让 newA 和 newC 脱离该方法,但它给了我这个错误。如果数字从方法中出来,我也不知道如何分开,请帮忙,我还是个新手:( 到目前为止,代码从用户那里获得了 3 个数字。然后我将它们放入方法中,我需要取出 newA 和 newC 并将它们存储在单独的变量中。

namespace factorising_quadratic_expressions
{
    class Program
    {
    static void Main(string[] args)
    {
        int divideBy = 0;
        Console.WriteLine("The form is ax^2 + bx + c"); // all of this is part of my working out.
        Console.WriteLine("Write down a ");
        int a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Write down b ");
        int b = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Write down c ");
        int c = Convert.ToInt32(Console.ReadLine());
        int num = a * c;
        g(num, b, divideBy); // i need to make 2 variables with newA and newC here
    }
    public static int g(int num01, int num02, int num03)
    {
        int x = 0;
        while (x == 0)
        {
            if (num03 > 20)
            {
                Console.WriteLine("this is not possible to factorise");
                x = x + 1;
            }
            num03 = num03 + 1;
            int newA = num01 / num03; //calculations done to get newC and newA
            int newC = num03;
            if (newA + newC == num02)
            {
                x = x + 1;
            }
            else if (newA - newC == num02)
            {
                x = x + 1;
            }
            else if (newC - newA == num02)
            {
                x = x + 1;
            }
            num01 = newC;  //me failing at trying to return them back
            num03 = newA;
            return num01; return num02; return num03;


            }
        }
    }
}

【问题讨论】:

    标签: methods return return-value


    【解决方案1】:

    这是因为您在 while 语句之外没有返回值。编译器认为可能存在 x !== 0 然后跳过 while 循环的情况。该函数应该返回一个int,因此会导致错误。

    尝试将 return -1; 放在 while 循环之后(或其他可以让您知道函数有错误的数字。

    此外,您的 g() 函数底部不需要多个 return 语句。一旦到达第一个 return 语句,它将返回该数字,之后不会运行任何代码。

    如果您尝试将答案打印到屏幕上,您可以将 g() 函数设置为 void 函数并且不返回任何内容。现在在您的 main 函数中,无论如何您都没有对 g() 的返回值做任何事情。

    我会这样写你的 g() 函数:

    public void g(int num01, int num02, int num03)
    {
        if (num03 > 20)
        {
            Console.WriteLine("this is not possible to factorise");
            return;
        }
    
        num03 = num03 + 1;
        int newA = num01 / num03;
        int newC = num03;
    
        if (newA + newC == num02)
        {
            return;
        }
    
        if (newA - newC == num02)
        {
            return;
        }
    
        if (newC - newA == num02)
        {
            return;
        }
    
        // Now that we've got all the error cases taken care of,
        // you can do the math here and use console.WriteLine to
        // print it out to the screen.
    
        return;
    }
    

    如果我有任何语法错误,我深表歉意。我是凭记忆写的,没试过编译。

    【讨论】:

    • 我把它放进去,但是它不允许我打开程序,因为有错误。并非所有代码路径都返回值。
    • 好的,我发现这段代码还有一些其他问题。我将编辑我的答案以包括这些。
    • 我如何使它返回多个具有一个返回函数的变量?顺便求帮助
    • 谢谢,但我需要返回变量 newA 和 newC。感谢您抽出宝贵时间 :) 我想先让他们回来,然后再和他们一起做事。
    • g() 函数定义声明它返回一个 int。如果您需要返回多个数字,您可以创建一个不同的函数来计算每个数字。如果您对每个数字执行相同的操作,则可以有一个函数,但每次从主函数调用它时只需为其提供不同的参数。或者,如果操作只适合几行,最好将它们全部放在同一个函数中。 (另外,抱歉,我刚刚意识到我一直在将这些方法称为“函数”,因为我最近做了很多 PHP 编程。)
    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多