public void First() { Console.WriteLine("这是我第一个函数的第一句话。");
} static void Main(string[] args) {
Program hanshu = new Program(); //实例化 初始化 这个类 //hanshu.First(); }
//格式一:没有参数,没有返回值(无参无返) /// <summary> /// 累加求和,没有参数没有返回值 /// </summary> public void LeiJia() { Console.Write("请输入一个正整数:"); int a = int.Parse(Console.ReadLine()); int sum=0; for (int i = 1; i <= a;i++ ) { sum += i; } Console.WriteLine("sum="+sum); } static void Main(string[] args) { Program hanshu = new Program(); hanshu.LeiJia();//格式1调用 }
1 //格式二:有参数,没有返回值(有参无返) 2 /// <summary> 3 /// 4 /// </summary> 5 /// <param name="a"></param> 6 public void LeiJia(int a) 7 { 8 int sum = 0; 9 for (int i = 1; i <= a; i++) 10 { 11 sum += i; 12 } 13 Console.WriteLine("sum=" + sum); 14 } 15 static void Main(string[] args) 16 { 17 Program hanshu = new Program(); 18 Console.Write("请输入一个正整数:"); 19 int a = int.Parse(Console.ReadLine()); 20 hanshu.LeiJia(a);//格式2调用 21 }
1 /格式三:有参数,有返回值(有参有返) 2 /// <summary> 3 /// 4 /// </summary> 5 /// <param name="d"></param> 6 /// <returns></returns> 7 public int LeiJia1(int d) 8 { 9 int sum = 0; 10 for (int i = 1; i <= d; i++) 11 { 12 sum += i; 13 } 14 return sum; 15 } 16 static void Main(string[] args) 17 { 18 Program hanshu = new Program(); 19 Console.Write("请输入一个正整数:"); 20 int a = int.Parse(Console.ReadLine()); 21 int sum=hanshu.LeiJia1(a); 22 Console.WriteLine("sum=" + sum); //格式3调用 23 }
1 //格式四:没有参数,有返回值(无参有返) 2 /// <summary> 3 /// 4 /// </summary> 5 /// <returns></returns> 6 public int LeiJia2() 7 { 8 Console.Write("请输入一个正整数:"); 9 int a = int.Parse(Console.ReadLine()); 10 int sum = 0; 11 for (int i = 1; i <= a; i++) 12 { 13 sum += i; 14 } 15 return sum; 16 } 17 static void Main(string[] args) 18 { 19 Program hanshu = new Program(); 20 int sum = hanshu.LeiJia2(); 21 Console.WriteLine("sum=" + sum); //格式4调用 22 }
1 //比较大小 2 /// <summary> 3 /// 输出最大的值 4 /// </summary> 5 /// <param name="a"></param> 6 /// <param name="b"></param> 7 /// <returns></returns> 8 public double Max(double a,double b) 9 { 10 if (a > b) 11 { 12 return a; 13 } 14 else 15 { 16 return b; 17 } 18 } 19 static void Main(string[] args) 20 { 21 Program hanshu = new Program(); 22 double a = 10; 23 double b = 9; 24 double c = 15; 25 double d = hanshu.Max(hanshu.Max(a, b),c); 26 Console.WriteLine(d); 27 }
//求一元二次方程的根
1 class Class1 2 { 3 //格式一:无参无返 4 public void Jie() 5 { 6 Console.Write("a="); 7 double a = double.Parse(Console.ReadLine()); 8 Console.Write("b="); 9 double b = double.Parse(Console.ReadLine()); 10 Console.Write("c="); 11 double c = double.Parse(Console.ReadLine()); 12 if (a == 0) 13 { 14 Console.WriteLine("此方程不是一元二次方程!"); 15 } 16 else 17 { 18 double d=b*b-4*a*c; 19 double x1; 20 double x2; 21 if(d>0) 22 { 23 x1=(-b+Math.Sqrt(d))/(2*a); 24 x2=(-b-Math.Sqrt(d))/(2*a); 25 Console.WriteLine("方程有两个不等实根!x1={0},x2={1}",x1,x2); 26 } 27 else if(d==0) 28 { 29 x1=-b/(2*a); 30 Console.WriteLine("方程有两个相等实根!x1=x2={0}", x1); 31 } 32 else 33 { 34 Console.WriteLine("方程无实根"); 35 } 36 } 37 } 38 39 //格式二:有参无返 40 public void Jie(double a, double b, double c) 41 { 42 if (a == 0) 43 { 44 Console.WriteLine("此方程不是一元二次方程!"); 45 } 46 else 47 { 48 double d = b * b - 4 * a * c; 49 double x1; 50 double x2; 51 if (d > 0) 52 { 53 x1 = (-b + Math.Sqrt(d)) / (2 * a); 54 x2 = (-b - Math.Sqrt(d)) / (2 * a); 55 Console.WriteLine("方程有两个不等实根!x1={0},x2={1}", x1, x2); 56 } 57 else if (d == 0) 58 { 59 x1 = -b / (2 * a); 60 Console.WriteLine("方程有两个相等实根!x1=x2={0}", x1); 61 } 62 else 63 { 64 Console.WriteLine("方程无实根"); 65 } 66 } 67 } 68 69 //格式三:有参有返 70 public string Jie1(double a, double b, double c) 71 { 72 string m = ""; 73 if (a == 0) 74 { 75 m="此方程不是一元二次方程!"; 76 } 77 else 78 { 79 double d = b * b - 4 * a * c; 80 double x1; 81 double x2; 82 83 if (d > 0) 84 { 85 x1 = (-b + Math.Sqrt(d)) / (2 * a); 86 x2 = (-b - Math.Sqrt(d)) / (2 * a); 87 m = "方程有两个不等实根!x1="+x1+",x2="+x2; 88 } 89 else if (d == 0) 90 { 91 x1 = -b / (2 * a); 92 m="方程有两个相等实根!x1=x2="+ x1; 93 } 94 else 95 { 96 m="方程无实根"; 97 } 98 } 99 return m; 100 } 101 102 //格式四:无参有返 103 public string Jie2() 104 { 105 Console.Write("a="); 106 double a = double.Parse(Console.ReadLine()); 107 Console.Write("b="); 108 double b = double.Parse(Console.ReadLine()); 109 Console.Write("c="); 110 double c = double.Parse(Console.ReadLine()); 111 string m = ""; 112 if (a == 0) 113 { 114 m = "此方程不是一元二次方程!"; 115 } 116 else 117 { 118 double d = b * b - 4 * a * c; 119 double x1; 120 double x2; 121 122 if (d > 0) 123 { 124 x1 = (-b + Math.Sqrt(d)) / (2 * a); 125 x2 = (-b - Math.Sqrt(d)) / (2 * a); 126 m = "方程有两个不等实根!x1=" + x1 + ",x2=" + x2; 127 } 128 else if (d == 0) 129 { 130 x1 = -b / (2 * a); 131 m = "方程有两个相等实根!x1=x2=" + x1; 132 } 133 else 134 { 135 m = "方程无实根"; 136 } 137 } 138 return m; 139 } 140 141 static void b(string[] args) 142 { 143 Class1 hanshu = new Class1(); 144 //格式一: 145 //hanshu.Jie(); 146 147 //格式二: 148 //Console.Write("a="); 149 //double a = double.Parse(Console.ReadLine()); 150 //Console.Write("b="); 151 //double b = double.Parse(Console.ReadLine()); 152 //Console.Write("c="); 153 //double c = double.Parse(Console.ReadLine()); 154 //hanshu.Jie(a,b,c); 155 156 //格式三: 157 Console.Write("a="); 158 double a = double.Parse(Console.ReadLine()); 159 Console.Write("b="); 160 double b = double.Parse(Console.ReadLine()); 161 Console.Write("c="); 162 double c = double.Parse(Console.ReadLine()); 163 //string A = hanshu.Jie1(a, b, c); 164 //string[] n = A.Split(' '); 165 //foreach (string m in n) 166 //{ 167 // Console.WriteLine(m); 168 //} 169 string m = hanshu.Jie1(a, b, c); 170 Console.WriteLine(m); 171 172 //格式四: 173 //string m = hanshu.Jie2(); 174 //Console.WriteLine(m); 175 176 177 178 Console.ReadLine(); 179 }