zoukankan      html  css  js  c++  java
  • 3月15号 函数

    首先是对函数传值类型的复习,分别是没有返回值没有输入值,没有返回值有输入值,有返回值没有输入值,有返回值也有输入值

    以下是对于四种方式小练习,比较大小输出较大的

    /// <summary>
            /// 比较大小 输出较大的 没有返回值 没有输入值
            /// </summary>
            /// <param name="args"></param>
            public void bijiao()
            {
                Console.Write("请输入x=");
                double x = double.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                double y = double.Parse(Console.ReadLine());
                if (x < y)
                {
                    Console.WriteLine(y);
                }
                else
                {
                    Console.WriteLine(x);
                }                 
            }
    
            static void Main(string[] args)
            {
                //比较大小的问题,输出较大的
              
                Program pp = new Program();
                pp.bijiao();
                Console.ReadLine();
     /// <summary>
            /// 比较大小 有返回值 没有输入值
            /// </summary>
            /// <param name="args"></param>
            public double bijiao1()
            {
                Console.Write("请输入x=");
                double x = double.Parse(Console.ReadLine());
                Console.Write("请输入y=");
                double y = double.Parse(Console.ReadLine());
                double zhong;
                if (x < y)
                {
                    zhong = x;
                    x = y;
                    y = zhong;
                }
                else
                {
                  
                }
                return x;     
            }
    
            static void Main(string[] args)
            {
                //比较大小的问题,输出较大的
              
                //Program pp = new Program(); //没有返回值,没有输入值
                //pp.bijiao();
                //Console.ReadLine();
    
                Program pp = new Program();
                double x = pp.bijiao1();
                Console.WriteLine(x);
                Console.ReadLine();

    下面是没有用函数的方式写出的一个关于人机大战的游戏

      for (; ; )
                {
                    Console.WriteLine("猜拳游戏");
                    Console.WriteLine("1:剪子");
                    Console.WriteLine("2:包袱");
                    Console.WriteLine("3:锤");
                    Console.WriteLine("4:游戏结束");
                    Console.Write("请出拳(选择相应的数字即可)");
                    int a = int.Parse(Console.ReadLine());
                    Random ran = new Random();
                    int n = ran.Next(1,4);
                    if (a > 0 && a < 4)
                    {
                        switch (n)
                        { 
                            case 1:
                                Console.WriteLine("电脑出剪子");
                                break;
                            case 2:
                                Console.WriteLine("电脑出包袱");
                                break;
                            case 3:
                                Console.WriteLine("电脑出锤");
                                break;
                        }
                    }
                    if (a >= 1 && a <= 3)
                    {
                        if (a - n == -1 || a - n == 2)
                        {
                            Console.WriteLine("恭喜你赢了,回车继续");
                        }
                        if (a - n == 1 || a - n == -2)
                        {
                            Console.WriteLine("很遗憾你输了,回车继续");
                        }
                        if (a - n == 0)
                        {
                            Console.WriteLine("平局,回车继续");
                        }
                    }
                    else
                    {
                        if (a == 4)
                        {
                            break;
                        }
                        Console.WriteLine("输入有误,回车继续");
                        Console.ReadLine();
                    }
                }

    以下会是函数的方式做

     /// <summary>
            /// 人机游戏,没有输入 没有返回值
            /// </summary>
            /// <param name="args"></param>
            public void caiquan()
            {
                for (; ; )
                {
                    Console.WriteLine("猜拳游戏");
                    Console.WriteLine("1:剪子");
                    Console.WriteLine("2:包袱");
                    Console.WriteLine("3:锤");
                    Console.WriteLine("4:结束");
                    Console.Write("请出拳:选择相应的号码即可");
                    int a = int.Parse(Console.ReadLine());
                    Random ran = new Random();
                    int n= ran.Next(1,4);
                    if (n > 0 && n < 4)
                    {
                        switch (n)
                        { 
                            case 1:
                                Console.WriteLine("电脑出剪子");
                                break;
                            case 2:
                                Console.WriteLine("电脑出包袱");
                                break;
                            case 3:
                                Console.WriteLine("电脑出锤");
                                break;
                        }
                    }
                    if (a >= 1 && a <= 3)
                    {
                        if (a - n == -1 || a - n == 2)
                        {
                            Console.WriteLine("恭喜你赢了,回车继续");
                        }
                        if (a - n == -2 || a - n == 1)
                        {
                            Console.WriteLine("很遗憾你输了,回车继续");
                        }
                        if (a - n == 0)
                        {
                            Console.WriteLine("平局,回车继续");
                        }
                        Console.ReadLine();
                    }
                    else
                    {
                        if (a == 4)
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("输入错误,回车继续");
                            Console.ReadLine();
                        }
                    }
    
                }
            }
    
              static void Main(string[] args)
            {
                Program pp = new Program();  //没有返回值 没有输入值
                pp.caiquan();
                Console.ReadLine();
    
     public void fangcheng(double a,double b,double c,out double x1,out double x2)
            {
                 double de = b * b - 4 * a * c;
                 if (de > 0)
                 {
                     Console.WriteLine("方程有两个不相等的实根");
                     x1 = (-b + Math.Sqrt(de) / 2 * a);
                     x2 = (-b - Math.Sqrt(de) / 2 * a);
                 }
                 else
                 {
                     Console.WriteLine("方程有两个相等的实根");
                     x1 = -b / 2 * a;
                     x2 = x1;
                 }       
            }
          
            static void Main(string[] args)
            {
                double x1;
                double x2;
                Console.WriteLine("求a*x*x+b*x+c=0");
                Console.Write("请输入a=");
                double a = double.Parse(Console.ReadLine());
                if (a == 0)
                {
                    Console.WriteLine("这不是一元二次方程");
                }
                else
                {
                    Console.Write("请输入b=");
                    double b = double.Parse(Console.ReadLine());
                    Console.Write("请输入c=");
                    double c = double.Parse(Console.ReadLine());
                    double de = b * b - 4 * a * c;
                    if (de < 0)
                    {
                        Console.WriteLine("方程没有实根");
                    }
                    else
                    {
                        Program cc= new Program();
                        cc.fangcheng(a,b,c,out x1,out x2);
                        if (x1 == x2)
                        {
                            Console.WriteLine("x1=x2=" + x1);
                        }
                        else
                        {
                            Console.WriteLine("x1="+x1);
                            Console.WriteLine("x2="+x2);
                        }
                    }
                }
                Console.ReadLine();
  • 相关阅读:
    jquery保存用户名和密码到cookie里面
    avalon框架
    mybatis分页插件
    获取前台查询条件的公用方法
    mybatis分页插件
    maven出错The folder is already a source folder
    Jquery图片上传预览效果
    springMVC文件上传
    自动将String类型的XML解析成实体类
    JavaScript 引擎
  • 原文地址:https://www.cnblogs.com/Duriyya/p/5288280.html
Copyright © 2011-2022 走看看