using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { double a, b, c,X1,X2; //定义变量 Console.WriteLine("--------求一元二次方程:ax^2+bx+c=0 --------"); Console.WriteLine("请输入一个值给a:"); a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入一个值给b:"); b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入一个值给c:"); c = Convert.ToDouble(Console.ReadLine()); X1=(-b+Math.Sqrt(b*b-4*a*c))/2*a; X2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / 2 * a; if (b * b - 4 * a * c < 0) Console.WriteLine("此方程无实数解!"); else if (b * b - 4 * a * c == 0) Console.WriteLine("只有一个解!X1=X2={0}",X2=X1); else Console.WriteLine("X1的值为:{0}\tX的值为:{1}", X2, X2); Console.ReadKey(); } } }
时间: 2024-10-09 17:02:44