格式一:(无参无返)
public void fangcheng() { Console.WriteLine("请输入a的值"); double a = int.Parse(Console.ReadLine()); Console.WriteLine("请输入b的值"); double b = int.Parse(Console.ReadLine()); Console.WriteLine("请输入c的值"); double c = int.Parse(Console.ReadLine()); double de = b * b - 4 * a * c; if (a != 0) { if (de > 0) { double x1 = ((-b) + Math.Sqrt(de)) / 2 * a; double x2 = ((-b) - Math.Sqrt(de)) / 2 * a; Console.WriteLine("方程有两个解,分别是{0},{1}", x1, x2); } else if (de == 0) { double x = (-b) / 2 * a; Console.WriteLine("此方程有一个实数解,是{0}", x); } else { Console.WriteLine("此方程无解!"); } } else { Console.Write("此方程不是一元二次方程"); } }
//格式1
//Program hanshu = new Program();
//hanshu.fangcheng();
格式二:有参无返
public void fangcheng1(double a,double b,double c) { double de = b * b - 4 * a * c; if (a != 0) { if (de > 0) { double x1 = ((-b) + Math.Sqrt(de)) / 2 * a; double x2 = ((-b) - Math.Sqrt(de)) / 2 * a; Console.WriteLine("方程有两个解,分别是{0},{1}", x1, x2); } else if (de == 0) { double x = (-b) / 2 * a; Console.WriteLine("此方程有一个实数解,是{0}", x); } else { Console.WriteLine("此方程无解!"); } } else { Console.Write("此方程不是一元二次方程"); } } //格式2 // Program hanshu = new Program(); // hanshu.fangcheng1(a,b,c);
格式三:(有参有返)
public string fangcheng2(double a, double b, double c,out string jie) { double de = b * b - 4 * a * c; if (a != 0) { if (de > 0) { double x1 = ((-b) + Math.Sqrt(de)) / 2 * a; double x2 = ((-b) - Math.Sqrt(de)) / 2 * a; jie="方程有两个解,分别是"+ x1+ x2; } else if (de == 0) { double x = (-b) / 2 * a; jie = "此方程有一个实数解,是{0}" + x; } else { jie="此方程无解!"; } } else { jie="此方程不是一元二次方程"; } return jie; } //格式3 //Console.WriteLine("请输入a的值"); //double a = int.Parse(Console.ReadLine()); //Console.WriteLine("请输入b的值"); //double b = int.Parse(Console.ReadLine()); //Console.WriteLine("请输入c的值"); //double c = int.Parse(Console.ReadLine()); //string jie; //Program hanshu = new Program(); //hanshu.fangcheng2(a,b,c,out jie); //Console.WriteLine(jie);
格式四:(无参有返)
public string fangcheng3() { Console.WriteLine("请输入a的值"); double a = int.Parse(Console.ReadLine()); Console.WriteLine("请输入b的值"); double b = int.Parse(Console.ReadLine()); Console.WriteLine("请输入c的值"); double c = int.Parse(Console.ReadLine()); double de = b * b - 4 * a * c; string jie; if (a != 0) { if (de > 0) { double x1 = ((-b) + Math.Sqrt(de)) / 2 * a; double x2 = ((-b) - Math.Sqrt(de)) / 2 * a; jie = "方程有两个解,分别是" + x1 + x2; } else if (de == 0) { double x = (-b) / 2 * a; jie = "此方程有一个实数解,是{0}" + x; } else { jie = "此方程无解!"; } } else { jie = "此方程不是一元二次方程"; } return jie; } //格式4 //Program hanshu = new Program(); //string jie=hanshu.fangcheng3(); //Console.WriteLine(jie);