函数:能够独立完成某项功能的模块。
函数四要素:输入、输出、函数体、函数名
函数定义:
(static/public) 返回类型 函数名(参数类型 参数名,参数类型 参数名)
{
函数体
}
函数的调用:
返回变量类型 变量名 = 函数(实参值)
案例:输入一个数求阶乘(写成函数调用)
public void Jie()
{
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
Console.Write("阶乘结果是:" + jie);
Console.ReadLine();
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要将这个类初始化一下
hanshu.Jie();
可写成:带传值的
/// <summary>
/// 求阶乘
/// </summary>
public void Jie(int a)
{
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
Console.Write("阶乘结果是:" + jie);
Console.ReadLine();
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要将这个类初始化一下
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
hanshu.Jie(a);
可以写成传值加返回值的:
/// <summary>
/// 求阶乘
/// </summary>
public int Jie(int a)
{
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
return jie;
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要将这个类初始化一下
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
Console.Write("阶乘结果是:" + hanshu.Jie(a));
Console.ReadLine();
可以写成不传值但是带返回值的:
/// <summary>
/// 求阶乘
/// </summary>
public int Jie()
{
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
return jie;
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要将这个类初始化一下
Console.Write("阶乘结果是:" + hanshu.Jie());
Console.ReadLine();
例:写一个返回最大值的函数。调用。
/// <summary>
/// 两个数比较大小返回大的
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public int Max(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要将这个类初始化一下
Console.WriteLine(hanshu.Max(hanshu.Max(a, b), c));
//函数不仅可以多次使用,还可以嵌套使用
细化
namespace 函数
{
class Program
{
格式1:无参无返
<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);
Console.ReadLine();
}
格式2:无参有返
public int LeiJia1()
{
累加求和
Console.Write("请输入一个正整数:");
int a = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
return sum;
}
格式3:有参有返
public int LeiJia2(int a)
{
累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
return sum;
}
格式4:有参无返
public void LeiJia3(int a)
{
累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
Console.WriteLine(sum);
Console.ReadLine();
}
有参数表示在函数体中不需要再去接收
有返回值表示,我在下文中还需要使用这个结果
在调用函数的时候需要定义一个相同数据类型的变量接收
函数,比较大小返回大的
public double Max(double a, double b)
{
if (a > b)
{
return a;
}
else//a<=b
{
return b;
}
}
函数可以嵌套使用,但是函数不可以嵌套写
public void you()//邮箱,无参无返
{
Console.Write("请输入邮箱:");
string yx = Console.ReadLine();
if(yx.Contains ("@"))
{
int a=yx.IndexOf ("@");
int b=yx.LastIndexOf ("@");
if (a == b)
{
if(!yx.StartsWith ("@"))
{
string c = yx.Substring(a);
if(c.Contains ("."))
{
string d=yx.Substring (a-1,1);
string e = yx.Substring(a, 1);
if(d!="."&&e!=".")
{
if(!yx.EndsWith ("."))
{
Console.WriteLine("您输入的邮箱格式正确");
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console .WriteLine ("邮箱输入错误");
}
Console.ReadLine();
}
public string you2() //无参有返
{
Console.Write("请输入邮箱:");
string yx = Console.ReadLine();
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您输入的邮箱格式正确");
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
return yx;
}
有参有返
public string you3(string yx)
{
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您输入的邮箱格式正确");
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
return yx;
}
有参无返
public void you4(string yx)
{
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您输入的邮箱格式正确");
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
}
else
{
Console.WriteLine("邮箱输入错误");
}
Console.ReadLine();
}
加10分,给数组中的
public double[] JiaFen(double[] score)
{
for (int i = 0; i < score.Length; i++)
{
score[i] += 10;
}
return score;
}
public int renzong = 0;
public int dianzong = 0;
public string Cai()
{
Console.Write("请输入您出什么拳(剪刀、石头、布):");
string shu = Console.ReadLine();
if (shu == "剪刀" || shu == "石头" || shu == "布")
{
int ren = 0;
switch (shu)
{
case "剪刀":
ren = 1;
break;
case "石头":
ren = 2;
break;
case "布":
ren = 3;
break;
}
Random ran = new Random();
int dian = ran.Next(1, 4);
switch (dian)
{
case 1:
Console.WriteLine("电脑出剪刀");
break;
case 2:
Console.WriteLine("电脑出石头");
break;
case 3:
Console.WriteLine("电脑出布");
break;
}
int jie = ren - dian;
if (jie == 0)
{
return "本轮平局!";
}
else if (jie == 1 || jie == -2)
{
return "本轮胜出!";
}
else
{
return "本轮失败!";
}
}
else
{
return "输入有误!";
}
}
public void tizhong()
{
Console.Write("请输入性别");
string s = Console.ReadLine();
Console.Write("请输入体重");
double t = double.Parse(Console.ReadLine());
Console.Write("请输入身高");
double g = double.Parse(Console.ReadLine());
if (s == "男")
{
double n = t - g + 100;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是标准体重");
}
else if (n > 3)
{
Console.WriteLine("您的体重偏胖");
}
else
{
Console.WriteLine("您的体重偏瘦");
}
}
else if (s == "女")
{
double n = t - g + 110;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是标准体重");
}
else if (n > 3)
{
Console.WriteLine("您的体重偏胖");
}
else
{
Console.WriteLine("您的体重偏瘦");
}
}
else
{
Console.WriteLine("您的输入有误");
}
Console.ReadLine();
}
static void Main(string[] args)
{
要求:写一个函数,计算体重是否标准
函数需要三个输入值,分别为性别、体重kg、身高cm
男:身高-100=体重±3kg
女:身高-110=体重±3kg
Console.Write("请输入性别");
string s = Console.ReadLine();
Console.Write("请输入体重");
double t = double.Parse(Console.ReadLine());
Console.Write("请输入身高");
double g = double.Parse(Console.ReadLine());
if (s == "男")
{
double n = t - g + 100;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是标准体重");
}
else if (n > 3)
{
Console.WriteLine("您的体重偏胖");
}
else
{
Console.WriteLine("您的体重偏瘦");
}
}
else if (s == "女")
{
double n = t - g + 110;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是标准体重");
}
else if (n > 3)
{
Console.WriteLine("您的体重偏胖");
}
else
{
Console.WriteLine("您的体重偏瘦");
}
}
else
{
Console.WriteLine("您的输入有误");
}
Console.ReadLine();
Program hanshu = new Program();
hanshu.tizhong();
首先对于所在的类进行初始化
Program hanshu = new Program();
hanshu.LeiJia();
int sum = hanshu.LeiJia1();
在这个数值上再加10分
sum += 10;
int sum= hanshu.LeiJia2(5);
Console.WriteLine(sum);
Console.ReadLine();
hanshu.LeiJia3(5);
Console.Write("请输入a=");
double a = double.Parse(Console.ReadLine());
Console.Write("请输入b=");
double b = double.Parse(Console.ReadLine());
Console.Write("请输入c=");
double c = double.Parse(Console.ReadLine());
double max = hanshu.Max( hanshu.Max(a,b),c);
Console.WriteLine(max);
Console.ReadLine();
输入邮箱账号,判断是否格式正确
Program han = new Program();
han.you();
string yx = han.you2( );
Console.WriteLine(yx);
Console.ReadLine();
Console.WriteLine("请输入邮箱地址");
string yx = han.you3(Console .ReadLine ());
Console.WriteLine(yx);
Console.ReadLine();
Console.WriteLine("请输入邮箱地址");
han.you4(Console .ReadLine ());
要求输入班级人数,根据人数输入每个人的分数
由于这个班都是少数民族,所以都需要加10分
加10分的过程需要用到函数
Console.Write("请输入班级人数:");
int a = int.Parse(Console.ReadLine());
double [] score =new double [a];
for (int i = 0; i < a; i++)
{
Console.Write("请输入第{0}个人的分数:",i+1);
score[i] = double.Parse(Console.ReadLine());
}
Console.WriteLine("所有学生分数输入完毕,请按回车键继续!");
Console.ReadLine();
初始化
Program hanshu = new Program();
score = hanshu.JiaFen(score);
foreach (double aa in score)
{
Console.WriteLine(aa);
}
Console.ReadLine();
猜拳:
人机对战
剪刀 1
石头 2
包袱 3
人输入:(剪刀、石头、布)
0 平局
1 赢了
-1 输了
-2 赢了
2 输了
Console.Write("请输入您出什么拳(剪刀、石头、布):");
string shu = Console.ReadLine();
if (shu == "剪刀" || shu == "石头" || shu == "布")
{
int ren = 0;
switch (shu)
{
case "剪刀":
ren = 1;
break;
case "石头":
ren = 2;
break;
case "布":
ren = 3;
break;
}
Random ran = new Random();
int dian = ran.Next(1, 4);
switch (dian)
{
case 1:
Console.WriteLine("电脑出剪刀");
break;
case 2:
Console.WriteLine("电脑出石头");
break;
case 3:
Console.WriteLine("电脑出布");
break;
}
int jie = ren - dian;
if (jie == 0)
{
Console.WriteLine("本轮平局!");
}
else if (jie == 1 || jie == -2)
{
Console.WriteLine("本轮胜出!");
}
else
{
Console.WriteLine("本轮失败!");
}
}
else
{
Console.WriteLine("输入有误!");
}
Console.ReadLine();
输入过程中若出错
for (int i = 1; i > 0; i++)
{
Console.Write("请输入是或者不是!");
string ss = Console.ReadLine();
if (ss == "是" || ss == "不是")
{
break;
}
else
{
Console.WriteLine("输入有误!请重新输入!");
}
}
输入年月日,查看时间日期格式是否正确
for (int i = 1; i > 0; i++)
{
Console.Write("请输入年份:");
int year = int.Parse(Console.ReadLine());
if (year >= 0 && year <= 9999)
{
for (int a = 1; a > 0; a++)
{
Console.Write("请输入月份:");
int month = int.Parse(Console.ReadLine());
if (month >= 1 && month <= 12)
{
for (int b = 1; b > 0; b++)
{
Console.Write("请输入日期:");
int day = int.Parse(Console.ReadLine());
if (day >= 1 && day <= 31)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
Console.WriteLine("您输入的日期格式正确,您输入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day == 31)
{
Console.WriteLine("您输入的日期错误,请重新输入!");
continue;
}
else
{
Console.WriteLine("您输入的日期格式正确,您输入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
}
else
{
if (day <= 28)
{
Console.WriteLine("您输入的日期格式正确,您输入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day == 29)
{
Console.WriteLine("您输入的日期格式正确,您输入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
Console.WriteLine("您输入的日期错误,请重新输入!");
continue;
}
}
else
{
Console.WriteLine("您输入的日期错误,请重新输入!");
continue;
}
}
}
}
}
else
{
Console.WriteLine("您输入的日期错误,请重新输入!");
continue;
}
}
a = -10;//跳出
}
else
{
Console.WriteLine("您输入的月份错误,请重新输入!");
continue;
}
}
}
else
{
Console.WriteLine("您输入的年份错误,请重新输入!");
continue;
}
i = -10; //跳出
}Console.ReadLine();