Several methods of exchanging two variables

                            **1.使用临时变量**
1.	            Console.WriteLine("请输入两个整数x,y");
2.	            int x = int.Parse(Console.ReadLine());//使用 int.parse(string s) 将输入的字符串为int值  
3.	            int y = Convert.ToInt32(Console.ReadLine());//使用Convert.ToInt32(string  value)  将输入的字符串转化为int值
4.	            Console.WriteLine("x={0},y={1}", x, y);
5.	            int temp;//定义一个临时变量temp
6.	            temp = x;  //将x的值赋给temp
7.	            x = y;     //将y的值赋给x
8.	            y = temp;  //将temp存放的x的值赋给y
9.	            Console.WriteLine("x={0},y={1}",x,y);

Knowledge: an input string into an int two methods: int.Parse (string s) and Convert.ToInt32 (string value). Temp define a temporary variable, temp is assigned the value of x, y and then assigns the value of x, then the value stored in the temp y is assigned to x; (can be understood: equivalent to an empty cup, a cup of coffee cup milk, milk and coffee exchange)

                            **2.使用加减法**
1.              Console.WriteLine("输入两个整数x,y");
2.	            int x = int.Parse(Console.ReadLine());
3.	            int y = Convert.ToInt32(Console.ReadLine());
4.	            Console.WriteLine("x={0},y={1}", x, y);
5.	            x = x + y;  //将x+y的和的值赋给x
6.	            y = x - y;  //这个x-y等于x和y的和减去原先y的值剩余原先x的值
7.	            x = x - y;  //这个x-y等于x+y的和减去y的值(y在上一步已经变成原先x的值),剩余原先的y的值
8.	            Console.WriteLine("x={0},y={1}", x, y);

Thinking: x + y and the value assigned to x (y value unchanged). x minus y to the original value is the value of x, this value is assigned to get the x y. Then subtract the value of x (actually the original value of x) y the previous entry, now get the value of x (the original value of y)

                  **3.使用方法(这里注意要用到ref)**
1.	static void Main(string[] args)
2.	{
3.	      Console.WriteLine("请输入两个整数x,y");
4.	      int x = int.Parse(Console.ReadLine());
5.	      int y = Int32.Parse(Console.ReadLine());
6.	      Console.WriteLine("x={0},y={1}", x, y);
7.	      Swap(ref x, ref y);                   //方法在用一个类时调用  注意要用关键字ref
8.	      Console.WriteLine("x={0},y={1}", x, y); 
9.	      Exchange.Swap(ref x,ref y);           //方法在不同类时的调用 注意要用关键字ref
10.	      Console.WriteLine("x={0},y={1}",x,y);   
11.	}
12.	static void Swap(ref int x, ref int y) //写一个方法,调用方法实现变量交换 这是在同一个类的情况
13.	{
14.	      int temp = x;
15.	      x = y;
16.	      y = temp;
17.	 }
18.	class Exchange
19.	        {
20.	        /// <summary>
21.	        /// 写一个方法,调用方法实现变量交换 这是不在同一个类的情况
22.	        /// </summary>
23.	        /// <param name="x"></param>
24.	        /// <param name="y"></param>
25.	        public static  void Swap(ref int x, ref int y)
26.	        {
27.	            int temp = x;
28.	            x = y;
29.	            y = temp;
30.	        }
31.	        
32.	    }

ps: Note here that exchange of two variables using a method, use ref keyword (either argument or parameter). In the same class, to a method defined as static (static class), if not it will require a static class object reference. In different categories, in addition to the class is defined as static, plus modifiers public (without default if it should be private), otherwise can not refer to this method.

Published 17 original articles · won praise 0 · Views 742

Guess you like

Origin blog.csdn.net/weixin_44623941/article/details/104574039