当我尝试获取 x1 和 x2 的值时,控制台告诉我 x1 不是数字而 x2 是?
这是代码:
static void Main(string[] args)
{
long a, b, c;
double x1, x2, D;
Console.WriteLine("Write the numbers a, b, c: ");
string[] names = Console.ReadLine().Split(" ");
a = long.Parse(names[0]);
b = long.Parse(names[1]);
c = long.Parse(names[2]);
try
{
D = Math.Pow(b, 2) - 4 * a * c;
if (D > 0)
{
x1 = (-b + Math.Sqrt(D)) / (2.0 * a);
x2 = (-b - Math.Sqrt(D)) / (2.0 * a);
Console.WriteLine($"x1 = {x1}, x2 = {x2}");
}
if (D == 0)
{
x1 = (-b) / (2 * a);
if (a != 0) { Console.WriteLine($"x1 = x2 = {x1}");
}
if (a == 0) { Console.WriteLine($"x = {x1}"); }
}
if (D < 0)
{
Console.WriteLine("there are no roots");
}
}
catch
{
if (a == 0 && b == 0 && c == 0)
{
Console.WriteLine("x - any number");
}
else
{
Console.WriteLine("there are no solutions");
}
}
}
我改变了x1和x2的数据类型,这导致猫开始给我写信“没有解决方案”(在catch line上) 我用了数字 0 1 0 和 0 1 2
在数学中,我们永远不能除以 0,当 a 等于 0 时,您需要放置一个逻辑来处理该情况