zoukankan      html  css  js  c++  java
  • 7.31

    函数 有四种格式 

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

  • 相关阅读:
    GDI绘制时钟效果,与系统时间保持同步,基于Winform
    Asp.Net Core API网关Ocelot
    Docker打包 Asp.Net Core应用,在CentOS上运行
    【C#】数据库脚本生成工具(二)
    【C#附源码】数据库文档生成工具支持(Excel+Htm)
    微信小程序初使心得【微信小程序快速入门】
    论:开发者信仰之“天下IT是一家“(Java .NET篇)
    线程池,千万注意,原来很多人都在错用
    .NET跨平台之运行与Linux上的Jexus服务器
    StackExchange.Redis 之 SortedSet 类型示例
  • 原文地址:https://www.cnblogs.com/power8023/p/5726223.html
Copyright © 2011-2022 走看看