zoukankan      html  css  js  c++  java
  • 函数

    程序中的函数:
    什么是函数:一个独立的功能

    没有人规定写程序一定要用函数,但是这是一种约定,能让自己更方便的约定。

    函数的作用:提高代码的重用性,让代码的结构更加清晰;

    函数的四种状态:

    一、无返回值,无参数
    public static void 函数名()
    {
      函数体
    }

    二、无返回值,有参数
    public static void 函数名(参数类型 变量名)
    {
      函数体
    }

    三、有返回值,无参数
    public static 返回值类型 函数名()
    {
      上面一样的返回值类型 end = "";

      return end;
    }

    四、有返回值,有参数
    public static int 函数名(int a, int b)
    {
      int c = a + b;
      return c;
    }

    函数的注释:

    为了让第一次用的人知道这个函数是干什么的,需要哪些参数;
    在函数的上一行,直接写三个/ : ///
    会根据函数结构,自动生成以下注释内容:
    /// <summary>
    /// 针对于函数功能的介绍
    /// </summary>
    /// <param name="str">针对于传入参数的要求说明</param>
    /// <returns>返回值的内容介绍</returns>

    练习1

    用户输入012,返回石头剪子布

        class Program
        {
            public static string caiquan(int a)
            {
                string b = "";
                switch (a)
                {
                    case 1: b = "石头"; break;
                    case 2: b = "剪子"; break;
                    case 3: b = ""; break;
                }
                return b;
            }
            static void Main(string[] args)
            {
                int aaa = 0;
                int bbb = 0;
    
                while (true)
                {
                    Console.Write("请用户输入一个数(1,石头;2,剪子;3,布):");
                    int a = Convert.ToInt32(Console.ReadLine());
                    //用户赢    1,2    2,3    3,1
                    //电脑赢    1,3    2,1    3,2
                    //用户      -1     -1      2 
                    //电脑      -2      1      1
                    Random r = new Random();
                    int b = r.Next(1, 4);
    
                    int c = a - b;
                    string aa = caiquan(a);
                    string bb = caiquan(b);
    
                    if (c == -1 || c == 2)
                    {
                        Console.WriteLine("用户手势是:" + aa + ",电脑手势是:" + bb + "");
                        Console.WriteLine("用户赢");
                        aaa++;
                    }
                    else if (c == -2 || c == 1)
                    {
                        Console.WriteLine("用户手势是:"+aa+",电脑手势是:"+bb+"");
                        Console.WriteLine("电脑赢");
                        bbb++;
                    }
                    else if (c == 0)
                    {
                        Console.WriteLine("用户手势是:" + aa + ",电脑手势是:" + bb + "");
                        Console.WriteLine("平局");
                    }
                    if (aaa==2||bbb==2)
                    {
                        break;
                    }
                }
    
                Console.ReadLine();

    运算结果

    练习2

    输入一个string数组,返回string,数组索引0-n全部内容的拼接

        class Program
        {
            public static string shuzu(string[]a)
            {
                string end = "";
                for (int j = 0; j < a.Length; j++)
                {
                    end += a[j];
                }         
                return end;
            }
            static void Main(string[] args)
            {
             string[] b = new string[] { "a", "b", "c", "d", "c", "d" };
             Console.WriteLine((shuzu(b)));
    
                Console.ReadLine();
            }

    运算结果

    练习3

    创建一个学生结构体,姓名,性别,年龄,成绩,
    需要一个函数,把一个学生的结构体变量传进来,返回“姓名:xxx,性别:xxx,年龄:xxx,成绩:xxx。”

        class Program
        {
            public struct student
            {
                public string name;
                public string sex;
                public int age;
                public decimal score;
            }
            public enum sex
            {
                男,
                女
            }
            public static string jiegou(student aa)
            {
                string end = "";
    
                end = "姓名:"+aa.name+",性别:"+aa.sex+",年龄:"+aa.age+",成绩:"+aa.score+"";
    
                return end;
            }
            static void Main(string[] args)
            {
                student s = new student();
                s.name = "王五";
                s.sex = sex.男.ToString();
                s.age = 18;
                s.score = 78;
    
                Console.WriteLine((jiegou(s)));
    
                Console.ReadLine();
            }

    运算结果

    练习4

    传入一个字符串,返回“是纯数字”,“不是纯数字”

        class Program
        {
            public static string yanzheng(string s)
            {
                string end = "";
    
                try
                {
                    Convert.ToInt32(s);
                    Console.WriteLine("是纯数字");
                }
                catch
                {
                    Console.WriteLine("不是纯数字");
                }
    
                return end;
            }
            static void Main(string[] args)
            {
                string a = "1234a";
                Console.WriteLine((yanzheng (a)));
    
                Console.ReadLine();
            }

    运算结果

  • 相关阅读:
    SuperMap房产测绘成果管理平台
    SuperMap产权登记管理平台
    Android adb shell am 的用法(1)
    由浅入深谈Perl中的排序
    Android 内存监测和分析工具
    Android 网络通信
    adb server is out of date. killing...
    引导页使用ViewPager遇到OutofMemoryError的解决方案
    adb logcat 详解
    How to send mail by java mail in Android uiautomator testing?
  • 原文地址:https://www.cnblogs.com/sunshuping/p/5549564.html
Copyright © 2011-2022 走看看