题目:输入三个整数x,y,z,请把这三个数由小到大输出。
分析:不借组新的变量
编程实现:
import java.util.*;
public class Comparison{
public static void main(String args[]){
int x,y,z;
Scanner scanner = new Scanner(System.in);
System.out.println("Please input three number:");
System.out.print("第一个数为:");
x = scanner.nextInt();
System.out.print("第二个数为:");
y = scanner.nextInt();
System.out.print("第三个数为:");
z = scanner.nextInt();
if(x>y){
x = x^y;
y = x^y;
x = x^y;
}
if(x>z){
x = x^z;
z = x^z;
x = x^z;
}
if(y>z){
y = y^z;
z = y^z;
y = y^z;
}
System.out.println("三个数大小顺序:");
System.out.println(x+" "+y+" "+z);
}
}