zoukankan      html  css  js  c++  java
  • 2017-3-5 C#基础 函数

    函数/方法
    非常抽象
    独立完成某项功能的一个个体

    函数的作用:

    提高代码的重用性
    提高功能开发的效率
    提高程序代码的可维护性

    函数分为:

    固定功能函数
    高度抽象函数

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

    public static 返回值类型 函数名(需要的参数,第二个,int a,string b)
    {

    return 上面的返回值类型;
    }

    函数的多种形态
    1、有参数,有返回值
    2、无参数,无返回值
    3、有参数,无返回值
    4、无参数,有返回值

    函数返回多个值的办法
    ref - 可出可进
    out - 只出不进

    代表练习:

    1、定义一个函数,需要用户输入一个姓名
    输出 “xxx,你好啊!”

    namespace _2017_3_5函数___输入姓名
    {
        class Program
        {
            public static String nihao(String a)
            {
                String b = ""+a+",你好啊!”";
                return b;
            }
            static void Main(string[] args)
            {
                Console.Write("请输入您的姓名:");
               String c= Console.ReadLine();
    
               String s = Program.nihao(c);
               Console.Write(s);
               Console.ReadLine();
            }
        }
    }

    2、定义一个函数,需要用户输入两个姓名
    输出“xxx和xxx你们的缘分指数是(1-100),散了吧/缘分不错!”

    namespace _2017_3_5_函数___缘分指数
    {
        class Program
        {
            
            public static String yuanfen(string a, string b)
            {
                Random n = new Random();
                int d = n.Next(1, 100);
                string c="";
                if (d< 50)
                {
                    c = "" + a + "" + b + "你们的缘分指数是:" + d+",散了吧!";
                }
                else if(d>50&&d<100)
                {
                     c = "" + a + "" + b + "你们的缘分指数是:" + d + ",缘分不错!";
                }
                return c;
            }
           
            static void Main(string[] args)
            {
                
                Console.Write("请输入要匹配的姓名:");
                String e = Console.ReadLine();
                Console.Write("请输入要匹配的姓名2:");
                String f = Console.ReadLine();
                String s = Program.yuanfen(e,f);
                Console.Write(s);
                Console.ReadLine();
            }
        }
    }

    3、定义一个函数,计算乘除,需要用户输入两个数和一个运算符
    按照输入的运算符,对两个数进行相对的运算,输出结果

    namespace _2017_3_5___函数____计算加减乘除
    {
        class Program
        {
            public static int jisuan(int a,int b,string c)
            {int he=0;
            
                if(c=="+")
                {
                    he = a + b;
                }
                else if(c=="-")
                {
                     he = a - b;
                }
                else if(c=="*")
                {
                     he = a * b;
                }
                else if (c == "/")
                {
                     he = a / b;
                }
                   
                return he;
                
            }
            static void Main(string[] args)
            {
                Console.Write("请输入第一个数:");
                int a1=Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入第二个数:");
                int b1 = Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入运算符:");
                String c1 = Console.ReadLine();
                int s = Program.jisuan(a1, b1, c1);
                    Console.Write("结果是:"+s);
                Console.ReadLine();
              
            }
        }
    }

    4、猜拳方法
    定义一个猜拳方法,返回比试结果,需要输入两个手势
    手势可以是0 1 2

    输出结果:“选手1的手势是石头,选手2的手势是包袱,选手2获胜!”
    “手势有误!”

    CaiQuan(1,2);

    namespace _2017_3_5__函数____猜拳方法
    {
        class Program
        {
            public static string shoushi(string enda,string endb) 
            {
               
                String end = "";
                int a=0; int b=0;
                if (enda =="石头")
                {
                    a = 0;
                    
                }
                if (enda == "剪子")
                {
                    a = 1;
                   
                }
                if (enda == "包袱")
                {
                    a = 2;
                    
                }
                if (endb == "石头")
                {
                    b = 0;
                    
                }
                if (endb == "剪子")
                {
                    b = 1;
                   
                }
                if (endb == "包袱")
                {
                    b = 2;
                    
                }
                if ((enda == "石头" && endb == "剪子") || (enda == "剪子" && endb == "包袱") || (enda == "包袱" && endb == "石头"))
                {
                    end = "选手一胜利,选手二失败";
                   
                }
                else if ((enda == "石头" && endb == "石头") || (enda == "剪子" && endb == "剪子") || (enda == "包袱" && endb == "包袱"))
                {
                    end = "平局";
                   
                }
                else
                {
                    end = "选手一失败,选手二胜利";
                   
                }
                return end; 
                
            }
            static void Main(string[] args)
            {
                Console.Write("请输入选手一的手势:");
                String s = Console.ReadLine();
                Console.Write("请输入选手二的手势:");
                String ss = Console.ReadLine();
                String sss = Program.shoushi(s,ss);
                Console.Write(sss);
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    UVALive 3645 Objective: Berlin(最大流 :时序模型)
    【】筛选素数法
    UVaLive 7361(矩阵快速幂)
    【模板】KMP字符串匹配【KMP】
    【模板】KMP字符串匹配【KMP】
    【模板】KMP字符串匹配【KMP】
    八百标兵奔北坡【DP】
    八百标兵奔北坡【DP】
    八百标兵奔北坡【DP】
    八百标兵奔北坡【DP】
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6505859.html
Copyright © 2011-2022 走看看