0

Hello I'm a Beginner in C#. I have made this code for quadratic equation. It runs but does not give the right answer.

using System;
using System.Diagnostics;


namespace mynamespace
{
    class myclass
    {
        static void Main(string[] args)
        {
            float a, b, c, x1, x2;
            Console.Write("Enter Value in a");
                a=Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Enter Value in b");
                b=Convert.ToSingle(Console.ReadLine());
            Console.WriteLine("Enter Value in c");
                c=Convert.ToSingle(Console.ReadLine());

            x1=(-b + Math.Sqrt ( b*b - 4*a*c)/(2*a));
            x2=(-b - Math.Sqrt ( b*b - 4*a*c)/(2*a));
            Console.WriteLine(x1);
            Console.WriteLine(x2);
            Console.ReadKey();

                    }
    }
}
4

2 回答 2

7

您缺少一组括号;将您的代码更改为:

x1=((-b + Math.Sqrt ( b*b - 4*a*c))/(2*a));
x2=((-b - Math.Sqrt ( b*b - 4*a*c))/(2*a));
于 2013-09-20T00:30:13.563 回答
0

您不能将浮点数转换为字符串。你做:

a = float.Parse(Console.Readline());

你必须做一个 if 语句:

if((b*b - 4*a*c) < 0)
{
   Console.WriteLine("There are no real roots!");
}

然后将其余代码放在 else 语句中。

于 2013-10-15T15:58:20.410 回答