-12

我有以下 C# 代码:

double eps=0.1, low=1, y0=0, x, y, high, muchlat, answer, ribua;
Console.WriteLine("Enter x");
x = double.Parse(Console.ReadLine());
high = y = x;
muchlat = Math.Abs(y - y0) ;
if (x < 0)
{
    Console.WriteLine("X can't be less than zero, press any key to exit");

}
else if (muchlat > eps)
{

    while  (muchlat > eps)
    {
        Console.WriteLine(y);
        y0 = y;
        y = (high + low) /2;
        ribua = Math.Pow(y,2);
        if (ribua == x)
        {
            answer = x;
        }
        else if (ribua > x)
        {
            high =y;
        }
        else if (ribua < x)
        {
            low =y;
        }
        else if (muchlat < eps)
        {
            answer = y;
            break;
        }
    }
}
Console.WriteLine(answer);
Console.ReadLine();

当我尝试调试程序时,我收到此消息“使用未分配的局部变量'答案'(CS0165)”,我的问题是如何修复它,问题出在哪里?

4

2 回答 2

5

答案永远不会被初始化。

double eps=0.1, low=1, y0=0, x=0, y=0, high=0, muchlat=0, answer=0, ribua=0;
于 2012-11-29T20:07:11.710 回答
3

声明时只需定义答案为 0 的值。那应该解决它。

于 2012-11-29T20:07:01.570 回答