zoukankan      html  css  js  c++  java
  • 函数 及小练习

    函数:

    程序中的函数:
    什么是函数:一个独立的功能,比如,去饭店要土豆丝,不用管土豆丝怎么做的,只会吃就行。

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

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

    函数的四种状态:

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

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

    return end;
    }

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

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

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

             public static void yi()
            {
                Console.WriteLine("你好吗?");
                Console.WriteLine("我很好,你呢?");
                Console.WriteLine("我也很好!");
                Console.WriteLine("那就好!");
            }
            static void Main(string[] args)
            {
                yi();
                Console.WriteLine("------------------------");
                yi();
                Console.WriteLine("*************************");
                yi();
                Console.WriteLine("``````````````````````````");
                Console.ReadLine();
             }

    二、写一个函数,让这个函数返回 “今天中午吃啥?” 这句话;

            public static string er()
            {
                string a = "";
                Console.WriteLine("今天中午吃啥?");
                return a;//或者函数体直接写作  return "今天中午吃啥?";
            }
            static void Main(string[] args)
            {
                er();
                Console.ReadLine();
            }

    三、往函数中添加一个参数,打印这个数 +10的结果;

            public static void san(int i)
            {
                int[] san = new[] { 1, 2, 3, 5, 7, 8, 34, 67, 88, 99, 100 };
                Console.WriteLine((san[i] + 10));
                //或者函数体直接写作  Console.WriteLine((i+10));
            }
            static void Main(string[] args)
            {
                san(6);
                Console.ReadLine();
            }

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

            public static string si(int a)
            {
                string b = "";
                switch (a)
                {
                    case 0: b = "石头"; break;
                    case 1: b = "剪刀"; break;
                    case 2: b = ""; break;
                    default: b = "输入有误!"; break;
                }
                return b;
            }
            static void Main(string[] args)
            {
                Console.Write("请输入手势(0/1/2):");
                int d = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(si(d));
                Console.ReadLine();
            }

    五、三个数,返回这三个数的和

            public static void wu(int a, int b, int c)
            {
                Console.WriteLine((a + b + c));
            }
            static void Main(string[] args)
            {
                wu(3, 5, 8);
                Console.ReadLine();
            }

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

            public static string lv(string[] a)
            {
                string end = "";
                for (int i = 0; i < a.Length; i++)
                {
                    end += a[i];
                }
                Console.WriteLine(end);
                return end;
            }
            static void Main(string[] args)
            {
                string[] a = new string[] { "", "喜欢", "吃鱼", "", "", "" };
                lv(a);
                Console.ReadLine();
            }

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

            public struct Student            //第七题
            {
                public string Name;
                public string Sex;
                public int Old;
                public decimal score;
            }
            public enum Sex
            {
                男,
                女
            }
            public static string qi(Student ss)
            {
                string end = "";
                end = " 姓名:" + ss.Name + ",性别:" + ss.Sex + ",年龄:" + ss.Old + ",成绩:" + ss.score + "";
                return end;
            }
            static void Main(string[] args)
            {
                Student s = new Student();
                s.Name = "小小";
                s.Sex = Sex.女.ToString();
                s.Old = 20;
                s.score = 99.99m;
                Console.WriteLine(qi(s));//第七题
                Console.ReadLine();
            }

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

            public static string ba(string a)
            {            
                string end = "";
                try
                {
                    Convert.ToInt32(a);
                    end = "是纯数字";
                }
                catch
                {
                    end = "不是纯数字";
                }
                return end;
            }
            static void Main(string[] args)
            {
                string aa = "abcd12345";
                Console.WriteLine(ba(aa));
    
                Console.ReadLine();
            }

  • 相关阅读:
    Python-类和实例
    Python之操作文件和目录
    Python之split()函数
    Python之切片操作
    PyCharm导入selenium的webdirver模块出错
    Python编写“去除字符串中所有空格”
    Python编写“求一元二次方程的解”
    android开发学习 ------- 【转】Genymotion 小白安装
    android开发学习 ------- git
    android开发学习 ------- MongoDB数据库简单理解
  • 原文地址:https://www.cnblogs.com/123lucy/p/5548979.html
Copyright © 2011-2022 走看看