for (int i = 0; i <10; i++) {
Console.Title = "一元二次方程程序"; double a, b, c, d, x1, x2;
Console.WriteLine("求解一元二次方程ax*x+bx+c=0"); Console.WriteLine("请输入a:"); a = Convert.ToDouble(Console.ReadLine()); if (a == 0) { Console.WriteLine("不能为0,否则不是一元二次方程!!"); Console.WriteLine("请重新输入a:"); a = Convert.ToDouble(Console.ReadLine()); } else { Console.WriteLine("请输入b:"); b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入c:"); c = Convert.ToDouble(Console.ReadLine()); d = b * b - 4 * a * c;
if (d < 0) { Console.WriteLine("方程无实解"); } else { x1 = (-b + Math.Sqrt(d)) / (2 * a); x2 = (-b - Math.Sqrt(d)) / (2 * a); if (d == 0) { Console.WriteLine("方程有两个相同的实根:"); Console.WriteLine("x1=x2=" + x1.ToString()); } else { Console.WriteLine("方程有两个相同的实根:"); Console.WriteLine("x1=" + x1.ToString()); Console.WriteLine("x2=" + x2.ToString()); } } Console.WriteLine("按任意键重新开始。"); Console.ReadKey(); }
}
|
请发表评论