.net c#方法返回错误

Method return error
2021-03-07
  •  译文(汉语)
  •  原文(英语)

嗨,我在返回平均值时出现错误;如果任何人都可以帮助我解决我的问题,请说返回键后不能有对象表达式,我非常感谢.

    static void Pos(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100);
            if(sum >= 0)
            {
                sum += array[i];
                counter++;
            }
            float avg = sum / counter;
            return avg; <<< error?
        }
    }
解决过程1

您无法返回任何东西,因为您的返回类型为 void

更改 static void Pos(int[] array)static float Pos(int[] array)

而且,您可能会得到"并非所有代码路径返回";

做这个:

static float Pos(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        float avg;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100);
            if(sum >= 0)
            {
                sum += array[i];
                counter++;
            }
            avg = sum / counter;
        }
        return avg;
    }
速聊1:
我这样做了,然后在static void >>> Pos <<<遇到错误,因为不是所有代码路径都可以返回,我该怎么办?
解决过程2

您使用的是空白,不允许您返回任何内容.

解决过程3

您方法的返回类型为void,您正在从中返回一个值.

像这样做:

static float Pos(int[] array)
{
    Random rand = new Random();
    float sum = 0;
    int counter = 0;
    float avg = 0;
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = rand.Next(-100, 100);
        if(sum >= 0)
        {
            sum += array[i];
            counter++;
        }
        avg = sum / counter;

    }
    return avg;
}

当您将返回类型设置为void时,这意味着您不希望该方法返回任何内容.

速聊1:
我确实给了我static void >>> Pos <<<错误,不是所有代码路径都可以返回,我该怎么做?
速聊2:
在循环外返回表格

Hi I am getting an error at return avg; saying return key must not be followed by object expression if anyone could help me with my problem I be very thankful.

    static void Pos(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100);
            if(sum >= 0)
            {
                sum += array[i];
                counter++;
            }
            float avg = sum / counter;
            return avg; <<< error?
        }
    }
Solutions1

You cannot return anthing since your return type is void

Change this static void Pos(int[] array) to static float Pos(int[] array)

And also you may get "not all code path returns";

Do this:

static float Pos(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        float avg;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100);
            if(sum >= 0)
            {
                sum += array[i];
                counter++;
            }
            avg = sum / counter;
        }
        return avg;
    }
Talk1:
I did that then I get a error at static void >>> Pos <<< saing not all code path can be returned what do i do to that?
Solutions2

You're using a void which doesn't allow you to return anything.

Solutions3

Your method's return type is void, and you are returning a value from it.

Do it like this:

static float Pos(int[] array)
{
    Random rand = new Random();
    float sum = 0;
    int counter = 0;
    float avg = 0;
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = rand.Next(-100, 100);
        if(sum >= 0)
        {
            sum += array[i];
            counter++;
        }
        avg = sum / counter;

    }
    return avg;
}

When you set the return type to void, it means you don't want the method to return anything.

Talk1:
I did gives me static void >>> Pos <<< error saying not all code path can be returned what do i do to that?
Talk2:
return form outside for loop
转载于:https://stackoverflow.com/questions/23003835/method-return-error

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入