0505.Net基础班第七天(函数)

1、我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者, 管Test()函数称之为被调用者。 如果被调用者想要得到调用者的值: 1)、传递参数。 2)、使用静态字段来模拟全局变量。 如果调用者想要得到被调用者的值: 1)、返回值

2、 不管是实参还是形参,都是在内存中开辟了空间的。

3、方法的功能一定要单一。 GetMax(int n1,int n2) 方法中最忌讳的就是出现提示用户输入的字眼。

4、out、ref、params 1)、out参数。 如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。 但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候, 我们可以考虑使用out参数。 out参数就侧重于在一个方法中可以返回多个不同类型的值。

2)、ref参数 能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。 ref参数要求在方法外必须为其赋值,而方法内可以不赋值。

3)、params可变参数 将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。 params可变参数必须是形参列表中的最后一个元素。

5、方法的重载 概念:方法的重载指的就是方法的名称相同给,但是参数不同。 参数不同,分为两种情况 1)、如果参数的个数相同,那么参数的类型就不能相同。 2)、如果参数的类型相同,那么参数的个数就不能相同。 ***方法的重载跟返回值没有关系。

6、方法的递归 方法自己调用自己。 找出一个文件夹中所有的文件。

01复习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01复习
 8 {
 9     public enum Gender
10     {
11         男,
12 13     }
14 
15     public struct Person
16     {
17         public string _name;
18         public int _age;
19         public Gender _gender;
20     }
21 
22     class Program
23     {
24         static void Main(string[] args)
25         {
26 
27             //Person zsPerson;
28             //zsPerson._name = "张三";
29             //zsPerson._age = 20;
30             //zsPerson._gender = Gender.男;
31 
32             //Gender gender = Gender.男;
33             //string s = "男";
34 
35             //Gender g = (Gender)Enum.Parse(typeof(Gender), s);
36             //Console.WriteLine(g);
37             //Console.ReadKey();
38 
39 
40             //常量  枚举  结构  数组
41             //  const int number = 10;
42 
43 
44             //数组 可以一次性的存储多个相同类型的变量
45           //  int[] numbers = new int[10];
46 
47            // numbers[20] = 3;
48 
49             //冒泡排序
50 
51 
52             int[] numbers = { 11, 2, 55, 64, 3, 9, 17 };
53             //升序排列
54             Array.Sort(numbers);
55             Array.Reverse(numbers);
56             for (int i = 0; i < numbers.Length; i++)
57             {
58                 Console.WriteLine(numbers[i]);
59             }
60             Console.ReadKey();
61 
62             //for (int i = 0; i < numbers.Length-1; i++)
63             //{
64             //    for (int j = 0; j < numbers.Length-1-i; j++)
65             //    {
66             //        if (numbers[j] < numbers[j + 1])
67             //        {
68             //            int temp = numbers[j];
69             //            numbers[j] = numbers[j + 1];
70             //            numbers[j + 1] = temp;
71             //        }
72             //    }
73             //}
74 
75             //for (int i = 0; i < numbers.Length; i++)
76             //{
77             //    Console.WriteLine(numbers[i]);
78             //}
79             Console.ReadKey();
80 
81 
82         }
83     }
84 }
View Code

02方法的调用问题

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _02方法的调用问题
 8 {
 9     class Program
10     {
11         //字段 属于类的字段
12         public static int _number = 10;
13 
14 
15         static void Main(string[] args)
16         {
17             //  int b = 10;
18             int a = 3;
19             int res = Test(a);
20             Console.WriteLine(res);
21             // Console.WriteLine(_number);
22             Console.ReadKey();
23         }
24         public static int Test(int a)
25         {
26             a = a + 5;
27             return a;
28             //  Console.WriteLine(_number);
29         }
30 
31 
32         public static void TestTwo()
33         {
34             // Console.WriteLine(_number);
35         }
36     }
37 }
View Code

03判断闰年

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03判断闰年
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //举例:写一个方法,判断一个年份是否是润年.
14 
15             bool b = IsRun(2000);
16             Console.WriteLine(b);
17             Console.ReadKey();
18 
19         }
20         /// <summary>
21         /// 判断给定的年份是否是闰年
22         /// </summary>
23         /// <param name="year">要判断的年份</param>
24         /// <returns>是否是闰年</returns>
25         public static bool IsRun(int year)
26         {
27             bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
28             return b;
29         }
30 
31 
32     }
33 }
View Code

04方法概念

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _04方法概念
 8 {
 9     class Program
10     {
11         public static void Main(string[] args)
12         {
13             //比较两个数字的大小 返回最大的
14             //int a1 = 10;
15             //int a2 = 20;
16             int max = GetMax(10, 20);//实参
17             Console.WriteLine(max);
18             Console.ReadKey();
19         }
20 
21 
22         /// <summary>
23         /// 计算两个整数之间的最大值 并且返回最大值
24         /// </summary>
25         /// <param name="n1">第一个数</param>
26         /// <param name="n2">第二个数</param>
27         /// <returns>返回的最大值</returns>
28         public static int GetMax(int n1, int n2)//形参
29         {
30             int max = n1 > n2 ? n1 : n2;
31 
32             return max;
33         }
34     }
35 }
View Code

05方法练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _05方法练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //1 读取输入的整数
14             //多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
15 
16             Console.WriteLine("请输入一个数字");
17             string input = Console.ReadLine();
18             int number = GetNumber(input);
19             Console.WriteLine(number);
20             Console.ReadKey();
21         }
22 
23         /// <summary>
24         /// 这个方法需要判断用户的输入是否是数字
25         /// 如果是数字,则返回
26         /// 如果不是数字,提示用户重新输入
27         /// </summary>
28         public static int GetNumber(string s)
29         {
30             while (true)
31             {
32                 try
33                 {
34                     int number = Convert.ToInt32(s);
35                     return number;
36                 }
37                 catch
38                 {
39                     Console.WriteLine("请重新输入");
40                     s = Console.ReadLine();
41                 }
42             }
43         }
44 
45 
46     }
47 }
View Code

06方法的3个练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _06方法的3个练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //2 还记得学循环时做的那道题吗?只允许用户输入y或n,请改成方法
14 
15             //这个方法做了什么事儿?
16             //只能让用户输入yes或者no,只要不是,就重新输入
17 
18             //输入yes 看 输入no 不能看   
19 
20             //Console.WriteLine("请输入yes或者no");
21             //string str = Console.ReadLine();
22             //string result = IsYerOrNo(str);
23             //Console.WriteLine(result);
24             //Console.ReadKey();
25 
26             //4计算输入数组的和:int Sum(int[] values)
27             int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
28             int sum = GetSum(nums);
29             Console.WriteLine(sum);
30             Console.ReadKey();
31         }
32 
33         /// <summary>
34         /// 计算一个整数类型数组的总和
35         /// </summary>
36         /// <param name="nums">要求总和的数组</param>
37         /// <returns>返回这个数组的总和</returns>
38         public static int GetSum(int[] nums)
39         {
40             int sum = 0;
41             for (int i = 0; i < nums.Length; i++)
42             {
43                 sum += nums[i];
44             }
45             return sum;
46         }
47 
48 
49 
50         /// <summary>
51         /// 限定用户只能输入yes或者no 并且返回
52         /// </summary>
53         /// <param name="input">用户的输入</param>
54         /// <returns>返回yes或者no</returns>
55         public static string IsYerOrNo(string input)
56         {
57             while (true)
58             {
59                 if (input == "yes" || input == "no")
60                 {
61                     return input;
62                 }
63                 else
64                 {
65                     Console.WriteLine("只能输入yes或者no,请重新输入");
66                     input = Console.ReadLine();
67                 }
68             }
69         }
70     }
71 }
View Code

07out参数

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _07out参数
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             //写一个方法 求一个数组中的最大值、最小值、总和、平均值
 14             int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 15             ////将要返回的4个值,放到一个数组中返回
 16 
 17             //int[] res = GetMaxMinSumAvg(numbers);
 18             //Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", res[0], res[1], res[2], res[3]);
 19             //Console.ReadKey();
 20             int max1;
 21             int min1;
 22             int sum1;
 23             int avg1;
 24             bool b;
 25             string s;
 26             double d;
 27             Test(numbers, out max1, out min1, out sum1, out avg1, out b, out s, out d);
 28 
 29             Console.WriteLine(max1);
 30             Console.WriteLine(min1);
 31             Console.WriteLine(sum1);
 32             Console.WriteLine(avg1);
 33             Console.WriteLine(b);
 34             Console.WriteLine(s);
 35             Console.WriteLine(d);
 36             Console.ReadKey();
 37 
 38         }
 39         /// <summary>
 40         /// 计算一个数组的最大值、最小值、总和、平均值
 41         /// </summary>
 42         /// <param name="nums"></param>
 43         /// <returns></returns>
 44         public static int[] GetMaxMinSumAvg(int[] nums)
 45         {
 46             int[] res = new int[4];
 47             //假设 res[0] 最大值  res[1]最小值  res[2]总和  res[3]平均值
 48             res[0] = nums[0];//max
 49             res[1] = nums[0];//min
 50             res[2] = 0;//sum
 51             string name = "孙全";
 52             bool b = true;
 53             for (int i = 0; i < nums.Length; i++)
 54             {
 55                 //如果当前循环到的元素比我假定的最大值还大 
 56                 if (nums[i] > res[0])
 57                 {
 58                     //将当前循环到的元素赋值给我的最大值
 59                     res[0] = nums[i];
 60                 }
 61                 if (nums[i] < res[1])
 62                 {
 63                     res[1] = nums[i];
 64                 }
 65                 res[2] += nums[i];
 66             }
 67             //平均值
 68             res[3] = res[2] / nums.Length;
 69             return res;
 70 
 71 
 72         }
 73 
 74 
 75         /// <summary>
 76         /// 计算一个整数数组的最大值、最小值、平均值、总和
 77         /// </summary>
 78         /// <param name="nums">要求值得数组</param>
 79         /// <param name="max">多余返回的最大值</param>
 80         /// <param name="min">多余返回的最小值</param>
 81         /// <param name="sum">多余返回的总和</param>
 82         /// <param name="avg">多余返回的平均值</param>
 83         public static void Test(int[] nums, out int max, out int min, out int sum, out int avg, out bool b, out string s, out double d)
 84         {
 85             //out参数要求在方法的内部必须为其赋值
 86             max = nums[0];
 87             min = nums[0];
 88             sum = 0;
 89             for (int i = 0; i < nums.Length; i++)
 90             {
 91                 if (nums[i] > max)
 92                 {
 93                     max = nums[i];
 94                 }
 95                 if (nums[i] < min)
 96                 {
 97                     min = nums[i];
 98                 }
 99                 sum += nums[i];
100             }
101             avg = sum / nums.Length;
102 
103 
104             b = true;
105             s = "123";
106             d = 3.13;
107         }
108 
109 
110 
111     }
112 }
View Code

08使用out参数做登陆

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _08使用out参数做登陆
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //分别的提示用户输入用户名和密码
14             //你写一个方法来判断用户输入的是否正确
15             //返回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息
16             //如果用户名错误,除了返回登陆结果之外,还要返回一个 "用户名错误"
17             //“密码错误”
18             Console.WriteLine("请输入用户名");
19             string userName = Console.ReadLine();
20             Console.WriteLine("请输入密码");
21             string userPwd = Console.ReadLine();
22             string msg;
23             bool b = IsLogin(userName, userPwd, out msg);
24             Console.WriteLine("登陆结果{0}",b);
25             Console.WriteLine("登陆信息{0}",msg);
26             Console.ReadKey();
27         }
28 
29         /// <summary>
30         /// 判断登陆是否成功
31         /// </summary>
32         /// <param name="name">用户名</param>
33         /// <param name="pwd">密码</param>
34         /// <param name="msg">多余返回的登陆信息</param>
35         /// <returns>返回登陆结果</returns>
36         public static bool IsLogin(string name, string pwd, out string msg)
37         {
38             if (name == "admin" && pwd == "888888")
39             {
40                 msg = "登陆成功";
41                 return true;
42             }
43             else if (name == "admin")
44             {
45                 msg = "密码错误";
46                 return false;
47             }
48             else if (pwd == "888888")
49             {
50                 msg = "用户名错误";
51                 return false;
52             }
53             else
54             {
55                 msg = "未知错误";
56                 return false;
57             }
58         }
59     }
60 }
View Code

09自己动手写tryparse

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _09自己动手写tryparse
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int num;
14             bool b = int.TryParse("123abc", out num);
15             Console.WriteLine(num);
16             Console.WriteLine(b);
17             Console.ReadKey();
18         }
19 
20         public static bool MyTryParse(string s, out int result)
21         {
22             result = 0;
23             try
24             {
25                 result = Convert.ToInt32(s);
26                 return true;
27             }
28             catch
29             {
30                 return false;
31             }
32         }
33 
34 
35     }
36 }
View Code

10ref参数

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _10ref参数
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             double salary = 5000;
14             JiangJin(ref salary);
15             Console.WriteLine(salary);
16             Console.ReadKey();
17         }
18 
19         public static void JiangJin(ref double s)
20         {
21             s += 500;
22 
23         }
24 
25         public static void FaKuan(double s)
26         {
27             s -= 500;
28         }
29     }
30 }
View Code

11、ref练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _11_ref练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //使用方法来交换两个int类型的变量
14             int n1 = 10;
15             int n2 = 20;
16             //int temp = n1;
17             //n1 = n2;
18             //n2 = temp;
19             Test(ref n1, ref  n2);
20             Console.WriteLine(n1);
21             Console.WriteLine(n2);
22             Console.ReadKey();
23             //n1 = n1 - n2;//-10 20
24             //n2 = n1 + n2;//-10 10
25             //n1 = n2 - n1;//20 10
26 
27         }
28 
29 
30         public static void Test(ref int n1, ref  int n2)
31         {
32             int temp = n1;
33             n1 = n2;
34             n2 = temp;
35         }
36     }
37 }
View Code

12params可变参数

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _12params可变参数
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //   int[] s = { 99, 88, 77 };
14             //Test("张三",99,100,100,100);
15             //Console.ReadKey();
16 
17             //求任意长度数组的和 整数类型的
18             int[] nums = { 1, 2, 3, 4, 5 };
19             int sum = GetSum(8,9);
20             Console.WriteLine(sum);
21             Console.ReadKey();
22         }
23 
24         public static int GetSum(params int[] n)
25         {
26             int sum = 0;
27             for (int i = 0; i < n.Length; i++)
28             {
29                 sum += n[i];
30             }
31             return sum;
32         }
33 
34 
35         public static void Test(string name, int id, params int[] score)
36         {
37             int sum = 0;
38 
39             for (int i = 0; i < score.Length; i++)
40             {
41                 sum += score[i];
42             }
43             Console.WriteLine("{0}这次考试的总成绩是{1},学号是{2}", name, sum, id);
44         }
45     }
46 }
View Code

13、方法的重载

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _13_方法的重载
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13        //   M()
14 
15             Console.WriteLine(1);
16             Console.WriteLine(1.4);
17             Console.WriteLine(true);
18             Console.WriteLine('c');
19             Console.WriteLine("123");
20             Console.WriteLine(5000m);
21             Console.ReadKey();
22         }
23         public static void M(int n1, int n2)
24         {
25             int result = n1 + n2;
26         }
27         //public static int M(int a1, int a2)
28         //{
29         //    return a1 + a2;
30         //}
31         public static double M(double d1, double d2)
32         {
33             return d1 + d2;
34         }
35         public static void M(int n1, int n2, int n3)
36         {
37             int result = n1 + n2 + n3;
38         }
39         public static string M(string s1, string s2)
40         {
41             return s1 + s2;
42         }
43 
44       
45     }
46 }
View Code

14、方法的递归

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _14_方法的递归
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             TellStory();
14             Console.ReadKey();
15         }
16 
17         //public static int i = 0;
18 
19 
20         public static void TellStory()
21         {
22             const int i = 0;
23             Console.WriteLine("从前有座山");
24             Console.WriteLine("山里有座庙");
25             Console.WriteLine("庙里有个老和尚和小和尚");
26             Console.WriteLine("有一天,小和尚哭了,老和尚给小和尚讲了一个故事");
27             i++;
28             if (i >= 10)
29             {
30                 return;
31             }
32              TellStory();
33            
34         }
35     }
36 }
View Code

15方法练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _15方法练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //            提示用户输入两个数字  计算这两个数字之间所有整数的和
14             //1、用户只能输入数字
15             //2、计算两个数字之间和
16             //3、要求第一个数字必须比第二个数字小  就重新输入
17             Console.WriteLine("请输入第一个数字");
18             string strNumberOne = Console.ReadLine();
19             int numberOne;
20             GetNumber(strNumberOne,out numberOne);
21             Console.WriteLine("请输入第二个数字");
22             string strNumberTwo = Console.ReadLine();
23             int numberTwo;
24             GetNumber(strNumberTwo,out numberTwo);
25 
26             //判断第一个数字是否小于第二个数字
27             JudgeNumber(ref numberOne, ref  numberTwo);
28 
29             //求和
30             int sum = GetSum(numberOne, numberTwo);
31             Console.WriteLine(sum);
32             Console.ReadKey();
33 
34 
35         }
36 
37 
38         public static void JudgeNumber(ref int n1, ref  int n2)
39         {
40             while (true)
41             {
42                 if (n1 < n2)
43                 {
44                     //复合题意
45                     return;
46                 }
47                 else//>=2
48                 {
49                     Console.WriteLine("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字");
50                     string s1 = Console.ReadLine();
51                     //调用GetNumber
52                     GetNumber(s1,out n1);
53                     Console.WriteLine("请重新输入第二个数字");
54                     string s2 = Console.ReadLine();
55                     GetNumber(s2,out n2);
56                 }
57 
58             }
59 
60         }
61         public static void GetNumber(string s,out int number)
62         {
63             while (true)
64             {
65                 try
66                 {
67                     number = Convert.ToInt32(s);
68                     return;
69                 }
70                 catch
71                 {
72                     Console.WriteLine("输入有误!!!请重新输入");
73                     s = Console.ReadLine();
74                 }
75             }
76         }
77 
78         public static int GetSum(int n1, int n2)
79         {
80             int sum = 0;
81             for (int i = n1; i <= n2; i++)
82             {
83                 sum += i;
84             }
85             return sum;
86         }
87     }
88 }
View Code
原文地址:https://www.cnblogs.com/liuslayer/p/4713370.html