用函数做 :判断质数、与7相关的数、阶乘、一元二次方程
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 函数 { class Program { /// <summary> /// 判断质数 /// </summary> /// <param name="a"></param> public void pdsbszs(int a) { int sum = 0; for (int i = 1; i <= a; i++) { int b = a % i; if (b == 0) { sum++; } } if (sum == 2) { Console.Write("这是个质数"); } else { Console.Write("这不是个质数"); } } /// <summary> /// 与7相关的数 /// </summary> /// <param name="s"></param> public void y7(string s) { int b = int.Parse(s); if (b % 7 == 0) { Console.WriteLine("这个数与7相关"); } else if (s.Contains("7")) { Console.WriteLine("这个数与7相关"); } else { Console.WriteLine("这个数与7无关"); } } /// <summary> /// 阶乘 /// </summary> /// <param name="j"></param> /// <returns></returns> public int jiechen(int j) { int sum=1; for(int i=1;i<=j;i++) { sum*=i; } return sum; } /// <summary> /// 一元二次方程 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> public void ecfc(double a,double b,double c,out double x1,out double x2) { x1 = 0; x2 = 0; if (a == 0) { Console.WriteLine("这不是一个一元二次方程:"); } else { double d = b * b - 4 * a * c; if (d > 0) { x1 = (-b - Math.Sqrt(d)) / (2 * a); x2 = (-b + Math.Sqrt(d)) / (2 * a); Console.WriteLine("这个方程有两个根:x1={0},x2={1}" , x1, x2); } else if (d == 0) { x1 = x2 = -b / 2 * a; Console.WriteLine("这个方程有一个根x1=x2={0}:",x1); } else { Console.WriteLine("这个方程没有根"); } } } static void Main(string[] args) { /* Console.Write("请输入一个数:"); int a = int.Parse(Console.ReadLine()); Program p = new Program(); p.pdsbszs(a);*/ /* Console.Write("请输入一个数:"); string a = Console.ReadLine(); Program p = new Program(); p.y7(a); Console.ReadLine();*/ /* Console.Write("请输入一个数:"); int a = int.Parse(Console.ReadLine()); Program j = new Program(); int n = j.jiechen(a); Console.WriteLine(n); Console.ReadLine();*/ while (true) { Console.Write("请输入a:"); double r = double.Parse(Console.ReadLine()); Console.Write("请输入b:"); double e = double.Parse(Console.ReadLine()); Console.Write("请输入c:"); double g = double.Parse(Console.ReadLine()); double x1; double x2; Program j = new Program(); j.ecfc(r, e, g, out x1, out x2); } Console.ReadLine(); } } }