• 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 + "" + b + "" + c);
    System.Threading.Thread.Sleep(100);
    }
    Console.Clear();
    Console.Write("开奖号码:" + qwe[0] + "" + qwe[1] + "" + 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();
    }




  • 相关阅读:
    declare handler 声明异常处理的语法
    mysql存储过程获取sqlstate message_text
    mongoTemplate操作内嵌文档
    mysql索引之七:组合索引中选择合适的索引列顺序
    mongoDB的操作总结
    explain之三:MYSQL EXPLAIN语句的extended 选项学习体会,分析诊断工具之二
    状态模式
    代码重构----使用java有限状态机来消除太多的if else判断
    断路器(CircuitBreaker)设计模式
    断路器之一:Hystrix 使用与分析
  • 原文地址:https://www.cnblogs.com/zblc2016/p/5723854.html
Copyright © 2020-2023  润新知