//1、没有返回值,没有参数 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); } //2、没有返回值,有参数 public void leijia(int z) { //累加求和 int sum = 0; for (int i = 1; i <= z; i++) { sum += i; } Console.WriteLine(sum); } //3、有返回值,有参数 public int leijia1(int z) { int sum = 0; for (int i = 1; i <= z; i++) { sum += i; } return sum; } //4、有返回值,没有参数 public int leijia2() { Console.Write("请输入一个整数:"); int a = int.Parse(Console.ReadLine()); int sum = 0; for (int i = 0; i <= a;i++ ) { sum += i; } return sum; } static void Main(string[] args) { //初始化这个类,是为了调用这个类里的函数 //函数必须通过调用,才能执行 Program hanshu = new Program(); hanshu.leijia(); Console.ReadLine(); Console.Write("请输入一个整数:"); int a = int.Parse(Console.ReadLine()); hanshu.leijia(a); Console.ReadLine(); Console.Write("请输入一个整数:"); int a1 = int.Parse(Console.ReadLine()); int sum = hanshu.leijia1(a1); Console.WriteLine(sum); Console.ReadLine(); int sum1 = hanshu.leijia2(); Console.WriteLine(sum1); Console.ReadLine();
/// <summary> /// 练习:两个值进行比较,返回较大的,若相等,返回任意一个 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public double max(double a, double b) { if (a > b) { return a; } else { return b; } } static void Main(string[] args) { Program hanshu = new Program(); double a = 4, b = 7, c = 2; Console.WriteLine(hanshu.max(hanshu.max(a,b),c)); Console.ReadLine(); }