Methods 并非所有代码路径都返回一个值,并且需要从一个方法中获取2个变量

Methods 并非所有代码路径都返回一个值,并且需要从一个方法中获取2个变量,methods,return,return-value,Methods,Return,Return Value,我试图从方法中取出newA和newC,但它给了我这个错误。我也不知道如何区分这些数字,如果它们来自这个方法,请帮助,我还是个新手:( 到目前为止,代码从用户那里得到了3个数字,然后我将它们放入方法中,我需要去掉newA和newC,并将它们存储在单独的变量中 namespace factorising_quadratic_expressions { class Program { static void Main(string[] args) { i

我试图从方法中取出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;


            }
        }
    }
}

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

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

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

如果您试图将答案打印到屏幕上,则可以将g()函数设置为
void
函数,并且不返回任何内容。现在在主函数中,您不会对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;
}

如果有语法错误,我深表歉意。我是从内存中编写的,没有尝试编译它。

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

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

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

如果您试图将答案打印到屏幕上,则可以将g()函数设置为
void
函数,并且不返回任何内容。现在在主函数中,您不会对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;
}

如果有语法错误,我深表歉意。我是从内存中编写的,没有尝试编译它。

我把它放进去了,但它不允许我打开程序,因为有错误。不是所有代码路径都返回值。好的,我看到这段代码还有一些其他问题。我将编辑我的答案以包含这些问题。我如何使它返回mo不止一个变量有一个返回函数?谢谢你的帮助,但我需要返回变量newA和newC。谢谢你的时间:)我想在处理它们之前尝试让它们返回。g()函数定义声明它返回一个int。如果需要返回多个数字,则可以创建不同的函数来计算每个数字。如果对每个数字执行相同的操作,则可以有一个函数,但每次从主函数调用时只需为其提供不同的参数。或者如果操作可以如果只需要几行代码,最好是将所有代码都保存在同一个函数中。(另外,对不起,我刚刚意识到我一直在调用这些方法“函数”,因为我最近做了很多PHP编程。)我把它放进去了,但它不允许我打开程序,因为有一个错误。不是所有的代码路径都返回一个值。好的,我看到这段代码还有一些其他问题。我将编辑我的答案以包括这些问题。如何使它用一个返回函数返回多个变量?谢谢你的帮助,但我需要返回变量谢谢你的时间:)我想试着让他们回来,然后再和他们一起做事函数定义声明它返回一个int。如果需要返回多个数字,则可以创建不同的函数来计算每个数字。如果对每个数字执行相同的操作,则可以有一个函数,但每次从主函数调用时只需为其提供不同的参数。或者如果操作可以如果只需要几行代码,最好是将所有代码都保存在同一个函数中。(另外,对不起,我刚刚意识到我一直在调用这些方法“函数”,因为我最近做了很多PHP编程。)