简体   繁体   English

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

[英]not all code paths return a value, and need to get 2 variables out of a method

I'm trying to get newA and newC out of the method but it gives me this error. 我正在尝试从方法中获取newA和newC,但这给了我这个错误。 I also don't know how to separate the numbers if they come out of the method please help, I am still a newbie :( so far the code gets 3 numbers from the user. I then put them into the method and I need to take away newA and newC and store them in separate variables. 我也不清楚如何从方法中分离出数字,请帮忙,我还是一个新手:(到目前为止,代码从用户那里得到了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;


            }
        }
    }
}

This is because you have no return value outside of your while statement. 这是因为在while语句之外没有返回值。 The compiler thinks that there could potentially be a case where x !== 0 and then the while loop would be skipped. 编译器认为,可能存在x!== 0的情况,然后将跳过while循环。 That function is supposed to return an int, so it would cause an error. 该函数应该返回一个int,因此将导致错误。

Try putting return -1; 尝试将return -1; after the while loop (or some other number that would let you know the function had an error. 在while循环之后(或其他一些可以让您知道函数出错的数字)。

Also, there is no need for the multiple return statements at the bottom of your g() function. 另外,您无需在g()函数底部使用多个return语句。 Once it gets to the first return statement it will return that number and no code after that will run. 一旦到达第一个return语句,它将返回该数字,此后将不运行任何代码。

If what you're trying to do is print out the answer to the screen, you can make your g() function a void function and not return anything. 如果您要执行的操作是在屏幕上打印出答案,则可以使g()函数成为void函数,而不返回任何内容。 Right now in your main function, you aren't doing anything with the return values from g() anyway. 现在,在您的main函数中,无论如何您都不会对g()的返回值做任何事情。

Here's how I would write your g() function: 这是我编写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;
}

My apologies if I have any syntax errors in there. 如果我有任何语法错误,我深表歉意。 I wrote it from memory and didn't try compiling it. 我是从内存中写出来的,没有尝试编译它。

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

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