1. 程式人生 > >java經典問題__將三個數從小到大輸出

java經典問題__將三個數從小到大輸出

題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。

public class Practise15 {

	public void sort(){
		Scanner scan = new Scanner(System.in);
		System.out.println("x=");
		int x = scan.nextInt();
		
		System.out.println("y=");
		int y = scan.nextInt();
		
		System.out.println("z=");
		int z = scan.nextInt();
		
		int temp;
		if(x > y){
			temp = y;
		    y = x;
		    x = temp;
		}//從小到大:x y(x 與 y的順序排列好了)
		
		if(x > z){
			System.out.println(z + " "+ x + " " + y);
		}else{
			if(y < z){
				System.out.println(x + " "+ y + " " + z);
			}else{
				System.out.println(x + " "+ z + " " + y);
			}
		}
		
		
		
	}
	
	
	public static void main(String[] args) {
		(new Practise15()).sort();

	}

}