您的位置:首页 > 编程语言 > C#

C#基础入门第七天(函数)

2020-12-31 17:26 1351 查看

1、我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,
管Test()函数称之为被调用者。
如果被调用者想要得到调用者的值:
1)、传递参数。
2)、使用静态字段来模拟全局变量。(多个方法都需要使用同一个变量)
语法:在类下面
public static int _number = 10;
如果调用者想要得到被调用者的值:
1)、返回值

class Program
{
////使用静态字段模拟全局变量
//public static int _number = 3;
static void Main(string[] args)
{
//int a = 3;
//int sum = Sum(a);
//Console.WriteLine(sum);
//Console.ReadKey();

//计算是否是闰年
Console.WriteLine("请输入一个年份,计算是否是闰年");
int year = Convert.ToInt32(Console.ReadLine());
bool b = IsRun(year);
Console.WriteLine(b);
Console.ReadKey();
}
/// <summary>
/// 接收用户输入,计算是否是闰年
/// </summary>
/// <param name="year">用户输入的年份</param>
/// <returns>是或否</returns>
public static bool IsRun(int year)
{
bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
return b;
}

///// <summary>
///// 计算a+5
///// </summary>
///// <param name="a">传进来的变量a</param>
///// <returns>返回计算后的结果</returns>
//public static int Sum(int a)
//{
//    return a = a + 5;
//}
}

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

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

方法的四个练习

class Program
{
static void Main(string[] args)
{
//1.读取输入的整数,定义成方法,多次调用(如果用户输入的数字,则返回,否则提示用户重新输入)
Console.WriteLine("请输入一个数字");
string input = Console.ReadLine();
int number = GetNumber(input);
Console.WriteLine(number);
Console.ReadKey();

//2.还记得学循环时做的那道题吗?只允许用户输入y/n,请改成方法
Console.WriteLine("请输入y/n");
string str = Console.ReadLine();
string strTwo = ShuRu(str);
Console.WriteLine(strTwo);
Console.ReadKey();

//3.查找两个整数中的最大值:intMax(int i1,int i2)
Console.WriteLine("对比两个数的最大值");
int i1 = 110;
int i2 = 20;
int max = GetMax(i1, i2);
Console.WriteLine(max);
Console.ReadKey();

//4.计算输入数组的和:int Sum(int[] values)
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int sum = GetSum(nums);
Console.WriteLine(sum);
Console.ReadKey();
}

#region 第一题的方法
/// <summary>
/// 将用户输入转换成数字并返回
/// 不能转换要求重新输。
/// </summary>
/// <param name="i">用户输入</param>
/// <returns></returns>
public static int GetNumber(string i)
{
while (true)
{
try
{
int number = Convert.ToInt32(i);
return number;
}
catch
{
Console.WriteLine("输入的内容不能转换,请重新输入");
i = Console.ReadLine();
}
}
}
#endregion

#region 第二题的方法
/// <summary>
/// 接收用户输入y/n
/// 如果是就返回,不是就重新输
/// </summary>
/// <param name="s">接收用户的输入</param>
/// <returns></returns>
public static string ShuRu(string s)
{
while (true)
{
if (s != "y" && s != "n")
{
Console.WriteLine("只能是y/n");
s = Console.ReadLine();
}
else
{
return s;
}
}
}
#endregion

#region 第三题的方法
/// <summary>
/// 对比两个数的最大值并且返回最大值
/// </summary>
/// <param name="n1">第一个整数</param>
/// <param name="n2">第二个整数</param>
/// <returns>返回的最大值</returns>
public static int GetMax(int n1,int n2)
{
return n1 > n2 ? n1 : n2;
}
#endregion

#region 第四题的方法
/// <summary>
/// 计算数组的和并返回
/// </summary>
/// <param name="n">传入的数组</param>
/// <returns>数组的和</returns>
public static int GetSum(int[] n)
{
int sum = 0;
for (int i = 0; i < n.Length; i++)
{
sum += i;
}
return sum;
}
#endregion
}

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

class Program
{
static void Main(string[] args)
{
//使用方法计算一个数组的最大值,最小值,总和,平均值
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = GetMaxMinSumAvg(nums);
Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均{3}", res[0], res[1], res[2], res[3]);
Console.ReadKey();
}

#region 一次返回多个同类型的值
/// <summary>
/// 求一个数组的最大值,最小值,总和,平均值
/// </summary>
/// <param name="n">传入的数组</param>
/// <returns></returns>
public static int[] GetMaxMinSumAvg(int[] n)
{
int[] res = new int[4];
//假设:res[0]最大值 res[1]最小值 res[2]总和 res[3]平均值
res[0] = n[0];
res[1] = n[0];
res[2] = 0;
for (int i = 0; i < n.Length; i++)
{
if (n[i] > res[0])
{
res[0] = n[i];
}
if (n[i] < res[1])
{
res[1] = n[i];
}
res[2] += n[i];
}
res[3] = res[2] / n.Length;
return res;
}
#endregion
}

out参数的使用

class Program
{
static void Main(string[] args)
{
//out参数的调用
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int max1;
int min1;
int sum1;
int avg1;
string s1;
bool b1;
double d1;
Test(nums, out max1, out min1, out sum1, out avg1, out s1, out b1, out d1);
Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均{3}", max1, min1, sum1, avg1);
Console.WriteLine(s1);
Console.WriteLine(b1);
Console.WriteLine(d1);
Console.ReadKey();
}

#region out参数的使用
/// <summary>
/// 计算一个数组的最大值,最小值,平均值,总和,在返回一个string类型,bool类型,double类型
/// </summary>
/// <param name="n">求值的数值</param>
/// <param name="max">最大值</param>
/// <param name="min">最小值</param>
/// <param name="sum">总和</param>
/// <param name="avg">平均值</param>
/// <param name="s">返回的string类型</param>
/// <param name="b">返回的bool类型</param>
/// <param name="d">返回的double类型</param>
public static void Test(int[] n,out int max,out int min,out int sum,out int avg, out string s, out bool b, out double d)
{
//out参数要求在方法的内部必须为其赋值
max = n[0];
min = n[0];
sum = 0;
for (int i = 0; i < n.Length; i++)
{
if (n[i] > max)
{
max = n[i];
}
if (n[i] < min)
{
max = n[i];
}
sum += n[i];
}
avg = sum / n.Length;
s = "abc";
b = true;
d = 3.13;
}
#endregion
}

out参数:方法内必须赋值,方法外不用,因为在方法内已经赋值了

class Program
{
static void Main(string[] args)
{
/*
分别提示用户输入用户名和密码,写一个方法来判断用户输入的是否正确
返回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息
如果用户名错误,除了返回登良路结果外,还要返回“用户名错误”,密码也一样
*/
Console.WriteLine("请输入用户名");
string userName = Console.ReadLine();
Console.WriteLine("请输入密码");
string userPwd = Console.ReadLine();
string msg1;
bool b = IsLogin(userName, userPwd, out msg1);
Console.WriteLine("登陆信息:{0}", b);
Console.WriteLine("登陆结果:{0}", msg1);
Console.ReadKey();

}
#region 登陆方法练习
/// <summary>
/// 判断登陆结果
/// </summary>
/// <param name="name">用户名</param>
/// <param name="pass">密码</param>
/// <param name="msg">登陆信息</param>
/// <returns>返回登陆结果</returns>
public static bool IsLogin(string name, string pass, out string msg)
{
if (name == "admin" && pass == "888888")
{
msg = "登陆成功";
return true;
}
else if (name == "admin")
{
msg = "密码错误";
return false;
}
else if (pass == "888888")
{
msg = "用户名错误";
return false;
}
else
{
msg = "全都错误";
return false;
}
}
#endregion
}

//使用方法实现TryParse
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串,转为数字");
string num = Console.ReadLine();
int num1;
bool b = TryParse(num, out num1);
Console.WriteLine(num1);
Console.WriteLine(b);
Console.ReadKey();
}
public static bool TryParse(string n,out int result)
{
result = 0;
try
{
result = Convert.ToInt32(n);
return true;
}
catch
{
return false;
}
}
}

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

class Program
{
static void Main(string[] args)
{
//ref参数调用
int n1 = 10;
int n2 = 20;
JiaoHuan(ref n1, ref n2);
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.ReadKey();
}
public static void JiaoHuan(ref int n1, ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}

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

class Program
{
static void Main(string[] args)
{
// int[] s = { 99, 98, 97 };
Test("张三", 1,99, 98, 97);
Console.ReadKey();
}

public static void Test(string name,int id, params int[] score)
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的总成绩是{1},学号{2}", name, sum, id);
}
}

class Program
{
static void Main(string[] args)
{

//求任意长度数组的和,整数类型
int[] nums1 = { 1, 2, 3 };
int sum = Nums(1, 2, 3, 4, 5, 6, 7, 8, 9);
Console.WriteLine(sum);
Console.ReadKey();
}

public static void Test(string name,int id, params int[] score)
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的总成绩是{1},学号{2}", name, sum, id);
}

public static int Nums(params int[] ns)
{
int sum = 0;
for (int i = 0; i < ns.Length; i++)
{
sum += ns[i];
}
return sum;
}
}

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

class Program
{
static void Main(string[] args)
{
string n = "姓";
string n1 = "名";
M(n,n1);
}

public static string M(string name,string waihao)
{
return name + waihao;
}
public static void M(int n1, int n2, int n3)
{
int result = n1 + n2 + n3;
}
public static double M(double d1, double d2)
{
return d1 + d2;
}
}

6、方法的递归
方法自己调用自己。
需要有一个条件,来停止调用

class Program
{
//定义一个全局字段来作为停止调用的条件,反正方法内,会被重置
public static int i = 0;
static void Main(string[] args)
{
Test();
Console.ReadKey();
}

public static void Test()
{
Console.WriteLine("从前有座山");
Console.WriteLine("山里有座庙");
Console.WriteLine("庙里有一个老和尚跟一个小和尚");
Console.WriteLine("老和尚对小和尚讲故事");
i++;
if (i >= 10)
{
return;
}
Test();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: