三星网站建设内容,超市小程序怎么做,中国企业500强中国铁建,建设旅游网站需要多少钱对于两种变量的交换,我发现四种方法,下面我用Java来演示一下。 1.利用第三个变量交换数值,简单的方法。 (代码演示一下) 1 class TestEV 2 //创建一个类 3 {4 public static void main(String[]args) 5 {6 int x =5,y=10; //定义两个变量 7 8 int temp = x; //定义第… 对于两种变量的交换,我发现四种方法,下面我用Java来演示一下。 1.利用第三个变量交换数值,简单的方法。 (代码演示一下) 1 class TestEV 2 //创建一个类 3 { 4 public static void main(String[]args) 5 { 6 int x =5,y=10; //定义两个变量 7 8 int temp = x; //定义第三临时变量temp并提取x值 9 x = y; //把y的值赋给x 10 y = temp; //然后把临时变量temp值赋给y 11 12 System.out.println("x="+x+"y="+y); 13 14 } 15 16 } 17 2.可以用两个数求和然后相减的方式进行数据交换,弊端在于如果 x 和 y 的数值过大的话,超出 int 的值会损失精度。 (代码演示一下) 1 class TestEV 2 //创建一个类 3 { 4 public static void main(String[]args) 5 { 6 int x