c# - 控制台应用程序代码错误

标签 c#

在我的代码中,

            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)': not all code paths return a value'.

我不确定代码有什么问题。解决方案?

最佳答案

非常简单。你的功能:

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

要求您返回一个整数。我想你是想这样做:

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

关于c# - 控制台应用程序代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692799/

相关文章:

c# - C# 中 Concurrency::combinable<T> 的模拟?

c# - 如何从 Selenium Driver.PageSource 获取 HtmlAgilityPack.HtmlDocument?

c# - 在运行时调整控件大小

c# - 如何在没有显式转换的情况下始终获得正确的类型

c# - Windows 窗体 : "System.ArgumentException: Parameter is not valid." coming from System stack

c# - 使用任务时如何延迟 Action 执行

c# - 通用应用程序缺少 WriteableBitmap SaveJpeg

c# - 构建将两个函数组合成一个复合函数的表达式树

c# - 检查列表中是否存在不区分大小写的字符串

c# - Count + List with Futures 不起作用