Why am I getting this error: not all code paths return a value?

前端 未结 5 1574
长发绾君心
长发绾君心 2021-01-16 21:15

hi im new to c# and was trying to code but getting error can anybody help me with this what am i doing wrong?

using System;
using System.Collections.Generic         


        
5条回答
  •  隐瞒了意图╮
    2021-01-16 22:11

    The first error, "not all code paths return a value" means there is a path that the code could follow where no value would be returned (ie: calling isHodder(1)). You need to return some value outside of the for loop. Additionally, since you have an if/else block inside the second for loop the line

    k=(2^j)-1;
    

    Will never be executed.

        static int isHodder(int n)
        {
            int k = n;
            for (int i = 2; i <= n / 2; i++)
            {
                if ((n % 1) == 0)
                {
                    return 0;
                }
                else
                {
                    for (int j = 2; j <= k; j++)
                    {
                        if (n == (2 ^ j) - 1)
                        {
                            return 1;
                        }
                        else
                        {
                            return 0;
                        }
                        k = (2 ^ j) - 1;
                    }
                }
            }
            return 0;
        }
    

提交回复
热议问题