1.不使用中间变量交换两个数
public class Exchange { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个整数:"); Long num1 = scanner.nextLong(); System.out.println("请输入一个整数:"); Long num2 = scanner.nextLong(); System.out.println("交换前num1:"+num1+" 交换前num2:"+num2 ); num1 = num1^num2; num2 = num2^num1; num1 = num1^num2; System.out.println("交换后num1:"+num1+" 交换后num2:"+num2 ); } }
2.利用三目运算符判断奇偶性
public class ParityCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个整数:"); Long num = scanner.nextLong(); String s = (num%2==0)?"这个数是:偶数":"这个数是:奇数"; System.out.println(s); } }