using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习函数 { class Program { //阶乘累加求和 //1.又返回值 ,并且有参数 public int jie(int z) { int sum = 0; int jie = 1; for (int i = 1; i <= z; i++) { jie *= i; sum += jie; } return sum; } static void Main(string[] args) { Program hanshu = new Program(); Console.Write("输入一个整数:"); int a = int.Parse(Console.ReadLine()); hanshu.jie(a); int sum = hanshu.jie(a); Console.WriteLine(sum); Console.ReadLine(); } //2.有返还值,没有参数 public int jie1() { Console.Write("输入一个整数"); int a = int.Parse(Console.ReadLine()); int sum = 0; int jie = 1; for (int i = 1; i <= a; i++) { jie *= i; sum += jie; } return sum; } static void Main(string[] args) { Program hanshu = new Program(); int sum = hanshu.jie1(); Console.WriteLine(sum); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hanshu { class Program { //没有返回值,没有参数 public void aa() { Console.WriteLine("Hello world!"); } //没有返回值,没有参数 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); } //没有返回值,有参数 public void Leijia(int z) { int sum = 0; for (int i = 1; i <= z; i++) { sum += i; } Console.WriteLine(sum); } //有返回值,并且有参数 public int Leijia1(int z) { int sum = 0; for (int i = 1; i <= z; i++) { sum += i; } return sum; } //有返回值,没有参数 public int Leijia2() { Console.Write("请输入一个整数:"); int a = int.Parse(Console.ReadLine()); int sum = 0; for (int i = 1; i <= a; i++) { sum += i; } return sum; } /// <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(); //hanshu.aa(); //函数必须经过调用才能执行 //hanshu.Leijia(); //Random ran = new Random(); //int aa =ran.Next(10); //累加求和 //Console.Write("请输入一个整数:"); //int a = int.Parse(Console.ReadLine()); ////hanshu.Leijia(a); //int sum= hanshu.Leijia1(a); //Console.WriteLine(sum); //int sum= hanshu.Leijia2(); //Console.WriteLine(sum); double a = 4, b = 7, c = 2; Console.WriteLine(hanshu.MAX(hanshu.MAX(a,b),c)); Console.ReadLine(); } } }