3.15课·········out传值(传址)

Posted 魅影星魂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.15课·········out传值(传址)相关的知识,希望对你有一定的参考价值。

public void Hs(out int a, out int b)
{
a = 4;
b = 6;
a = b++;//a=6,b=b+1=7//b先赋值给a,然后b+1
b = ++a;//a=a+1=7,b=7//a先加1,然后赋值给b
}

static void Main(string[] args)

{
int a = 0;
int b = 1;
Program hanshu = new Program();
hanshu.Hs(out a, out b);

Console.WriteLine("a=" + a+" ,b="+b);

Console.ReadLine();

//out相当于return返回值
//拿过来变量名的时候,里面默认为空值

out...可以返回多个值
//传值,只将这个变量的值给拿走,不返还,除非return赋值。
//将a的值传入函数,无论这个值在函数中如何变化,不会影响main中的a

//传址,将这个变量的值拿走运算,完成后还是得返还回来(不用return,用out)
//将a的值传入函数,函数中这个值如何变化,main中的a也会跟着变化
//相当于:函数中的这个值,是main中这个a的快捷方式

 题目:求解一元二次方程ax^2+bx+c=0的根的情况及结果

函数部分:

/// <summary>
        /// 求解一元二次方程ax^2+bx+c=0的根的情况及结果
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        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=");
            for (; ; )//循环输入,直至有解
            {
                double a = double.Parse(Console.ReadLine());
                if (a == 0)
                {
                    Console.WriteLine("此方程不是一元二次方程!");
                    Console.Write("请重新输入a=");
                }
                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("此方程没有实数根!");
                        Console.Write("请重新输入a=");
                    }
                    else//de>=0
                    {
                        Program pp = new Program();
                        pp.Fangcheng(a, b, c, out x1, out x2);

                        if (x1 == x2)
                        {
                            Console.WriteLine("x1=x2=" + x1);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("x1=" + x1);
                            Console.WriteLine("x2=" + x2);
                            break;
                        }
                    }                    
                }
}

题目:擂台比武(猜拳)

函数部分:

 /// <summary>
       /// 擂台比武,随机出招,看谁赢
       /// </summary>
       /// <param name="x"></param>
       /// <param name="d"></param>
       /// <param name="p"></param>
       /// <param name="pc"></param>
        public void  Caiquan(int x,out string d,out int p,out int pc)
        {  
             p = 0;  pc = 0;          
            Random ran = new Random();
            int y = ran.Next(1, 4);//1,2,3随机一个数
            switch (y)
            {
                case 1:
                    Console.WriteLine("看招,王麻子剪刀!");
                    break;
                case 2:
                    Console.WriteLine("吕布在此,谁敢放肆!");
                    break;
                case 3:
                    Console.WriteLine("我是石头他爸郭涛!");
                    break;
            }
            if (x - y == -1 || x - y == 2)
            { 
                p++;
                pc--;
                d = "你赢了!气死我了,你等着,我还会回来的!";
              
            }
            else if (x - y == 1 || x - y == -2)
            {
                p--;
                pc++;
                d = "你输了!哼哼,赶紧跪地求饶吧!"; 
               
            }
            else
            {
               d= "小样,有两下子,竟然能挡住我的龟波气功!";
            }
        }

程序部分:

 int p = 0; int pc = 0; string d; int sum = 0; int sum1 = 0;
           Console.WriteLine("剪子=1,布=2,石头=3");
           Console.WriteLine("擂台比武");
           int i = 1;
           for (; ; )
           {
               Console.Write("ROUND" + i + ",READY?GO:");
               int x = int.Parse(Console.ReadLine());
               if (x < 1 || x > 3)
               {
                   Console.WriteLine("你是在逗我开心吗!");
               }
               else
               {
                   Program s = new Program();
                   s.Caiquan(x, out d, out p, out pc);
                   Console.WriteLine(d);
                   sum += p;
                   sum1 += pc;
                   Console.WriteLine(sum + ":" + sum1);
               }
               i++;
           }

  题目:输入三个整数,xyz,最终以从大到小的方式输出.利用条件运算符

函数部分

 /// <summary>
         /// 输入三个整数,xyz,最终以从小到大的方式输出。利用条件运算符。
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
         public void Bt(out int x,out int y,out int z)
         {
             Console.Write("x=");
              x = int.Parse(Console.ReadLine());
             Console.Write("y=");
              y = int.Parse(Console.ReadLine());
             Console.Write("z=");
              z = int.Parse(Console.ReadLine());
             int x1 = (x < y) ? (x) : (y);
             int y1 = (x < y) ? (y) : (x);
             int x2 = (x1 < z) ? (x1) : (z);
             int z1 = (x1 <z) ? (z) : (x1);
             int y2 = (y1 < z1) ? (y1) : (z1);
             int z2 = (y1 < z1) ? (z1) : (y1);
             x = x2;
             y = y2;
             z = z2;
         }

程序部分

int x, y, z;
            Program n = new Program();
            n.Bt(out x,out y,out z);
            Console.WriteLine(x+"<"+y+"<"+z);

题目: 输入三个整数,xyz,最终以从大到小的方式输出.利用中间变量

函数部分

/// <summary>
        /// 输入三个整数,xyz,最终以从大到小的方式输出.利用中间变量
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="z1"></param>
        public void PaiXu( int x, int y, int z,out int x1,out int y1,out int z1)
        {
            int a;
            if (x < y)
            {
                a = x;
                x = y;
                y = a; //x,y调换//x>y           
            }
            if (x < z)
            {
                a = x;
                x = z;
                z = a;//x,z调换,此时x最大                 
            }
            if (y < z)
            {
                a = y;
                y = z;
                z = a;//y>z
            }
            x1 = x;
            y1 = y;
            z1 = z;
        }

程序部分

  int x1, y1, z1;            
            Console.Write("请输入第一个整数x=");
            int x = int.Parse(Console.ReadLine());
            Console.Write("请输入第二个整数y=");
            int y = int.Parse(Console.ReadLine());
            Console.Write("请输入第三个整数z=");
            int z = int.Parse(Console.ReadLine());           
            Program t = new Program();
            t.PaiXu(  x,  y,  z,out x1,out y1,out z1);
            Console.WriteLine(z1 + " " + y1 + " " + x1);//从小到大排列

 

 

 

以上是关于3.15课·········out传值(传址)的主要内容,如果未能解决你的问题,请参考以下文章

3.16课·········小结

3.12课·········数组

3.13课·········函数

3.30课·········Marquee标签

3.26课·········window.document对象

3.22课·········CSS样式表