• 函数 传址


    函数  传址

     

     

    1.输入两个数比较大小,返回大的

    class Program

        {

    public double bijiao()

            {

                Console.Write("请输入第一个数");

                double a = double.Parse(Console.ReadLine());

                Console.Write("请输入第二个数:");

                double b = double.Parse(Console.ReadLine());

                if (a > b)

                {

                    return a;

                }

                else

                {

                    return b;

                }

            }

    }

    static void Main(string[] args)

            {

                Program s = new Program();

                double c = s.bijiao2();

                Console.WriteLine("最大数:" + c);

                 Console.ReadLine();

            }

     

     

     

     

    2.猜拳游戏,剪刀石头布。

    class Program

        {

            /// <summary>

            ///

            /// </summary>

            public int fenshu=0;

            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 (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)

                        {

                            fenshu++;

                            Console.WriteLine("您赢了!请按回车继续!");

                            Console.WriteLine("分数=" + fenshu);

                        }

                        else if ((a - n) == -2 || (a - n) == 1)

                        {

                            fenshu--;

                            Console.WriteLine("您输了!请按回车继续!");

                            Console.WriteLine("分数=" + fenshu);

                        }

                        else

                        {

                            Console.WriteLine("平局!请按回车继续!");

                        }

                       

                        Console.ReadLine();

                    }

                    else

                    {

                        if (a == 4)

                        {

                            break;

                        }

                        Console.WriteLine("输入有误,请按回车继续!");

                        Console.ReadLine();

                    }

                }

            }

    }

    static void Main(string[] args)

            {

        Program cc = new Program();

          cc.Caiquan();

          Console.ReadLine();

        }

     

     

    传址:

            //传值,只将这个变量的值给拿走,不返还,除非return赋值。

            //将a的值传入函数,无论这个值在函数中如何变化,不会影响main中的a

           

            //传址,将这个变量的值拿走运算,完成后还是得返还回来(不用return,用out)

            //将a的值传入函数,函数中这个值如何变化,main中的a也会跟着变化

            //相当于:函数中的这个值,是main种这个a的快捷方式

     

     

    public void Jia(out int a , out int sum)

            {

                a = 3;

                sum = 5;

                sum = sum + a;

                a += sum;

            }

    static void Main(string[] args)

           

           {

                int q;

                int qq;

                Program pp = new Program();

                pp.Jia(out q, out qq);

                Console.WriteLine("sum=" + qq);

                Console.WriteLine("a=" + q);

                Console.ReadLine();

           }

    1、将求解一元二次方程改写成函数

        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)) / (a * 2);

                    x2 = (-b - Math.Sqrt(de)) / (a * 2);

                }

                else//de==0

                {

                    Console.WriteLine("此方程有两个相同的实数根!");

                    x1 = (-b + Math.Sqrt(de)) / (a * 2);

                    x2 = x1;

                }

            }

     

            static void Main(string[] args)

           

           {

               double x1;

               double x2;

               Console.WriteLine("求解一元二次方程ax^2+bx+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//de>=0

                   {

                       Program pp = new Program();

                       pp.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();

  • 相关阅读:
    【python】元组
    【python】列表
    1-读书的网站
    35-Python
    34-TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type 'tuple'
    33-wxpython多个frame之间的信息共享
    32-python代码打包成exe文件-pyinstaller
    31-字符串转为 url 格式的两种不同情况
    30-python3 中 bytes 和 string 之间的互相转换
    9-eclispe中右键BuildPath没有了
  • 原文地址:https://www.cnblogs.com/zst062102/p/5281192.html
Copyright © 2020-2023  润新知