【王道JAVA】【程序 15 排序】

题目:输入三个整数 x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如果 x>y 则将 x 与 y 的值进行交换,然后再用 x 与 z 进行比较,如果 x>z 则将 x 与 z 的值进行交换,这样能使 x 最小。

import java.util.Scanner;

public class WangDao {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter x: ");
		int x = scan.nextInt();
		System.out.print("Enter y: ");
		int y = scan.nextInt();
		System.out.print("Enter z: ");
		int z = scan.nextInt();
		
		int temp;
		if (x > y) {
			temp = y;
			y = x;
			x = temp;
		}
		if (x > z) {
			temp = z;
			z = x;
			x = temp;
		}
		if (y > z) {
			temp = z;
			z = y;
			y = temp;
		}
		System.out.println(x + "  " + y + "  " + z);
	}
}

猜你喜欢

转载自blog.csdn.net/YelloJesse/article/details/89375962