3

私のコードでは、

            int x;
            int y;
            x = 7;



            if (x == y)
            {
                Console.WriteLine("The numbers are the same!");

            }
            else
            {
                Console.WriteLine("The numbers are different.");
            }
            Console.ReadLine();


            for (int i = 0; i < y; i--)
            {
                Console.WriteLine("{0} sheep!", i);
            }
            Console.ReadLine();


            string[] colors = new string[y];
            colors[0] = "green";
            colors[1] = "yellow";
            colors[y] = "red";


            Console.WriteLine("Your new code is {0}.", Code(x, y));
            Console.ReadLine();

        }   

            static int Code(int myX, int myY)
            {
                int answer = myX * myX - myY;
            }
    }
}

次のようなエラーがあります。

「ConsoleApplication1.Program.Code(int, int)」: すべてのコード パスが値を返すわけではありません。

コードの何が問題なのかわかりません。ソリューション?

4

3 に答える 3

3

「answer」の値を返す必要があります。それ以外の場合、エラーが示すように、コードは値を返しません。

注: 'int' または 'string' を使用していた方法で使用する場合常に、値を返す必要があります。

static int Code(int myX, int myY)
    {
        int answer = myX * myX - myY;
        return answer;
    }
于 2013-10-30T20:08:24.670 に答える
2
static int Code(int myX, int myY)
{
     int answer = myX * myX - myY;
}

あなたの関数は結果を返しません(エラーが示すように)。そのはず:

static int Code(int myX, int myY)
{
    int answer = myX * myX - myY;
    return answer;
}
于 2013-10-30T20:07:59.050 に答える