编程求一元二次方程

来源:互联网 发布:腾讯游戏数据分析岗位 编辑:程序博客网 时间:2024/05/16 23:42
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            double x1 = 0;//解1            double x2 = 0;//解2            Console.WriteLine("求 ax^2+bx+c=0 的解");            Console.Write("请输入a的值:");            double a = Convert.ToInt32(Console.ReadLine());            Console.Write("请输入b的值:");            double b = Convert.ToInt32(Console.ReadLine());            Console.Write("请输入c的值:");            double c = Convert.ToInt32(Console.ReadLine());            double dt = b * b - 4 * a * c; //Δ的值            if (dt < 0)            {                Console.WriteLine("此方程无实数解.");            }            else if (dt == 0)            {                x1 = -b / 2 * a;                Console.WriteLine("方程的解为:x1=x2= " + x1.ToString("F"));  //保留小数点后两位            }            else            {                x1 = (-b + Math.Sqrt(dt)) / 2 * a;                x2 = (-b - Math.Sqrt(dt)) / 2 * a;                Console.WriteLine("方程的解为:x1= " + x1.ToString("F") + ", x2= " + x2.ToString("F"));            }            Console.ReadKey();        }    }}

 


总结:我通过这次学习懂得了编程求一元二次方程

0 0