不用第三个变量交换两个参数值得5种方法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
public static void main(String[] args) {
ChangeTest changeTest = new ChangeTest();
changeTest.changeMethodA(1, 10);
System.out.println("-----------------------------------");
}
public void changeMethodC(int a, int b){
System.out.println("changeMethodC交换之前\ta:"+a+"\tb:"+b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("changeMethodC交换之后\ta:"+a+"\tb:"+b);
changeTest.changeMethodD(4, 7);
System.out.println("-----------------------------------");
changeTest.changeMethodE(5, 6);
}
}
changeTest.changeMethodB(2, 9);
System.out.println("-----------------------------------");
changeTest.changeMethodC(3, 8);
System.out.println("-----------------------------------");
public void changeMethodA(int a, int b){
System.out.println("changeMethodA交换之前\ta:"+a+"\tb:"+b);
a = a + b - (b = a);
System.out.println("changeMethodA交换之后\ta:"+a+"\tb:"+b);
}
public void changeMethodD(int a, int b){
System.out.println("changeMethodD交换之前\ta:"+a+"\tb:"+b);
a = a * b;
b = a / b;
a = a / b;
System.out.println("changeMethodD交换之后\ta:"+a+"\tb:"+b);
}
public void changeMethodE(int a, int b){
System.out.println("changeMethodE交换之前\ta:"+a+"\tb:"+b);
a = a^b;
b = a^b;
a = a^b;
System.out.printห้องสมุดไป่ตู้n("changeMethodE交换之后\ta:"+a+"\tb:"+b);
package com.kevin.demo;
/**
* @author <a href="mailto:foohsinglong@gmail.com">kevin.long</a>
* @description 2011-12-11 14:22:55
*/
public class ChangeTest {
}
public void changeMethodB(int a, int b){
System.out.println("changeMethodB交换之前\ta:"+a+"\tb:"+b);
b = a + (a = b)*0;
System.out.println("changeMethodB交换之后\ta:"+a+"\tb:"+b);
public static void main(String[] args) {
ChangeTest changeTest = new ChangeTest();
changeTest.changeMethodA(1, 10);
System.out.println("-----------------------------------");
}
public void changeMethodC(int a, int b){
System.out.println("changeMethodC交换之前\ta:"+a+"\tb:"+b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("changeMethodC交换之后\ta:"+a+"\tb:"+b);
changeTest.changeMethodD(4, 7);
System.out.println("-----------------------------------");
changeTest.changeMethodE(5, 6);
}
}
changeTest.changeMethodB(2, 9);
System.out.println("-----------------------------------");
changeTest.changeMethodC(3, 8);
System.out.println("-----------------------------------");
public void changeMethodA(int a, int b){
System.out.println("changeMethodA交换之前\ta:"+a+"\tb:"+b);
a = a + b - (b = a);
System.out.println("changeMethodA交换之后\ta:"+a+"\tb:"+b);
}
public void changeMethodD(int a, int b){
System.out.println("changeMethodD交换之前\ta:"+a+"\tb:"+b);
a = a * b;
b = a / b;
a = a / b;
System.out.println("changeMethodD交换之后\ta:"+a+"\tb:"+b);
}
public void changeMethodE(int a, int b){
System.out.println("changeMethodE交换之前\ta:"+a+"\tb:"+b);
a = a^b;
b = a^b;
a = a^b;
System.out.printห้องสมุดไป่ตู้n("changeMethodE交换之后\ta:"+a+"\tb:"+b);
package com.kevin.demo;
/**
* @author <a href="mailto:foohsinglong@gmail.com">kevin.long</a>
* @description 2011-12-11 14:22:55
*/
public class ChangeTest {
}
public void changeMethodB(int a, int b){
System.out.println("changeMethodB交换之前\ta:"+a+"\tb:"+b);
b = a + (a = b)*0;
System.out.println("changeMethodB交换之后\ta:"+a+"\tb:"+b);