7.31 函数

函数的定义

一个较大的程序一般应分为若干个程序块,每一个模块用来实现一个特定的功能。所有的高级语言中都有子程序这个概念,用子程序实现模块的功能。

函数四要素:输入、输出、函数体、函数名

函数定义
(static/public) 返回类型 函数名(参数类型 参数名,参数类型 参数名)
{
函数体
}

void是没有返回值,括号内是参数。

函数的格式:

Ⅰ.无参无返

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();
}

static void Main(string[] args)
{

Program hanshu = new Program();//初始化

hanshu.LeiJia();

Ⅱ.无参有返

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;
}

static void Main(string[] args)
{

Program hanshu = new Program();//初始化

int sum= hanshu.LeiJia2();

Console.WriteLine(sum);
Console.ReadLine();

Ⅲ.有参有返
public int LeiJia2(int a)
{
//累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
return sum;
}

static void Main(string[] args)
{

Program hanshu = new Program();//初始化

int sum= hanshu.LeiJia2(5);

Console.WriteLine(sum);
Console.ReadLine();


Ⅳ.有参无返
public void LeiJia3(int a)
{
//累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
Console.WriteLine(sum);
Console.ReadLine();
}

static void Main(string[] args)
{

Program hanshu = new Program();//初始化

int sum= hanshu.LeiJia3(5);

注意:

有参数表示在函数体中不需要再去接收;有返回值表示,我在下文中还需要使用这个结果;在调用函数的时候需要定义一个相同数据类型的变量接收;函数可以嵌套使用,但是函数不可以嵌套写。
练习:

1.

要求:写一个函数,计算体重是否标准
函数需要三个输入值,分别为性别、体重kg、身高cm
男:身高-100=体重±3kg
女:身高-110=体重±3kg

public string sdf()    //采用的是格式2:无参有返
{
string o = "您是标准体重";
string m = "您的体重偏胖";
string n = "您的体重偏瘦";
string v = "您输入错误!";
Console.Write("请输入您的性别:");
string qaz = Console.ReadLine();
Console.Write("请输入您的体重kg:");
double wsx = double.Parse(Console.ReadLine());
Console.Write("请输入您的身高cm:");
double edc = double.Parse(Console.ReadLine());
if (qaz == "男")
{
double a = wsx - edc + 100;
if (a <= 3 && a >= -3)
{
return o;
}
else if (a > 3)
{
return m;
}
else
{
return n;
}


}
else if (qaz == "女")
{
double a = wsx - edc + 110;
if (a <= 3 && a >= -3)
{
return o;
}
else if (a > 3)
{
return m;
}
else
{
return n;
}

}
else
{
return v;

}

}
static void Main(string[] args)
{
Program hanshu = new Program();
string k = hanshu.sdf();
Console.WriteLine(k);
Console.ReadLine();

2.判断邮箱格式是否正确。

public string awe(string g)    //有参有返
{
string k = "您输入的邮箱账号有误!";
string h = "您输入的邮箱为" + g;
if (g.Contains("@"))
{
int a = g.IndexOf("@");
int b = g.LastIndexOf("@");
string w = g.Substring(a);
if (a == b)
{
bool c = g.StartsWith("@");
if (c == false)
{
bool d = g.EndsWith(".");
if (d == false)
{
if (w.Contains("."))
{
string e = g.Substring((a + 1), 1);
string f = g.Substring((a - 1), 1);
if (e != "." && f != ".")
{
return h;
}
else
{
return k;
}

}
else
{
return k;
}
}
else
{
return k;
}
}
else
{
return k;
}
}
else
{
return k;
}
}
else
{
return k;
}
}
static void Main(string[] args)
{
Program hanshu = new Program();
Console.Write("请输入邮箱账号:");
string s = hanshu.awe(Console.ReadLine());
Console.WriteLine(s);
Console.ReadLine();
}

3.猜拳:人机对战

剪刀 1  石头 2  包袱 3

人输入:(剪刀、石头、布)

0 平局    1 赢了    -1 输了   -2 赢了   2 输了

public void ess()                                                                        //无参无返
{
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();
}
static void Main(string[] args)
{
Program hanshu=new Program();

hanshu.ess();
}

4.福彩3d

public string qwe()
{
string o= "恭喜您,您中了1024元的直选奖项。";
string p="恭喜您,您中了346元的组选3奖项。";
string t="恭喜您,您中了173元的组选6奖项。";
string y="很遗憾您未中奖。";
string v = "您输入的号码有误!";
int[] qwe = new int[3];
Random ran = new Random();
qwe[0] = ran.Next(0, 10);
qwe[1] = ran.Next(0, 10);
qwe[2] = ran.Next(0, 10);
for (int i = 0; i < 10; i++)
{
Console.Clear();
int a = ran.Next(0, 10);
int b = ran.Next(0, 10);
int c = ran.Next(0, 10);
Console.Write("开奖号码:" + a + "\0" + b + "\0" + c);
System.Threading.Thread.Sleep(100);
}
Console.Clear();
Console.Write("开奖号码:" + qwe[0] + "\0" + qwe[1] + "\0" + qwe[2]);
Console.ReadLine();
Console.Write("请输入您购买的号码:");
string s = Console.ReadLine();
int d = s.Length;
if (d == 3)
{
string e = s.Substring(0, 1);
string f = s.Substring(1, 1);
string g = s.Substring(2, 1);
if (qwe[0].ToString() == e && qwe[1].ToString() == f && qwe[2].ToString() == g)
{
return o;
}
else
{
bool j = s.Contains(qwe[0].ToString());
bool h = s.Contains(qwe[1].ToString());
bool k = s.Contains(qwe[2].ToString());
if (j == true && h == true && k == true)
{
return p;
}
else if (j == true && h == true || j == true && k == true || h == true && k == true)
{
return t;
}
else
{
return y;
}
}
}
else
{
return v;
}
}
static void Main(string[] args)
{
Program hanshu = new Program();
string s = hanshu.qwe();
Console.WriteLine(s);
Console.ReadLine();
}




 

转载于:https://www.cnblogs.com/zblc2016/p/5723854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值