提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
不使用第三变量交换号码
/* from 时 代 J a v a 公 众 号 - N o w J a v a . c o m*/ public class Main { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); /** 时 代 J a v a - nowjava.com 提供 **/ //add both the numbers and assign it to first num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("Before Swapping");