public void changeVal(){ int a = 2; int b = 3; System.out.println("交换前 a:"+a+",b:"+b); a = a+b; b = a-b; a = a-b; System.out.println("交换后 a:"+a+",b:"+b); }…
#include <iostream> using namespace std; int main () { ; ; cout<<"a="<<a<<",b="<<b<<endl; a = a+b; ///a=7 b = a-b; ///b=3; a = a-b; ///a=5 cout<<"a="<<a<<",b="&l…
第一类方法也是常用的方法,通过多次的数值计算来完成交换,到现在知道的有下面三种: (1)加减法. a = a + b; b = a - b; a = a - b; 该方法可以交换整型和浮点型数值的变量,但在处理浮点型的时候有可能出现精度的损失,例如对数据: a = 3.123456 b = 1234567.000000 交换后各变量值变为: a = 1234567.000000 b = 3.125000 很明显,原来a的值在交换给b的过程中发生了精度损失. (2)乘除法. a = a * b;…