using System;
namespace 交换变量
{
class Program
{
static void Main(string[] args)
{
//交换变量,使用第三方变量
int a = 1;
int b = 2;
Console.WriteLine("a的值为{0},b的值为{1}", a, b);
int temp = a;
a = b;
b = temp;
Console.WriteLine("现在a的值为{0},b的值为{1}",a,b);
//不使用第三方变量
// 第一种:
int m = 5;
int n = 3;
Console.WriteLine("\nc的值为{0},d的值为{1}", m, n);
m = m - n;//此时m为两者的差
n = m + n;//此时n为初始m的值
m = n - m;//此时m为初始n的值
Console.WriteLine("现在a的值为{0},b的值为{1}",m, n);
// 第二种:
int n1 = 100;
int n2 = 10;
Console.WriteLine("\nn1的值为{0},n2的值为{1}", n1, n2);
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
Console.WriteLine("现在n1的值为{0},n2的值为{1}", n1, n2);
}
}
}
交换int,string,char,double都用第一种最简单