15 [程序 15 排序]
题目: 输入三个整数 x,y,z, 请把这三个数由小到大输出.
程序分析: 我们想办法把最小的数放到 x 上, 先将 x 与 y 进行比较, 如果 x>y 则将 x 与 y 的值进行交换, 然后再用 x 与 z 进行比较, 如果 x>z 则将 x 与 z 的值进行交换, 这样能使 x 最小.
- package cskaoyan;
- public class cskaoyan15 {
- private static int x;
- private static int y;
- private static int z;
- @org.junit.Test
- public void sort() {
- java.util.Scanner in = new java.util.Scanner(System.in);
- x = in.nextInt();
- y = in.nextInt();
- z = in.nextInt();
- if (x> y) {
- int temp = x;
- x = y;
- y = temp;
- }
- if (x> z) {
- int temp = x;
- x = z;
- z = temp;
- }
- if (y> z) {
- int temp = y;
- y = z;
- z = temp;
- }
- System.out.println(x + "<" + y + "<" + z);
- in.close();
- }
- }
来源: http://www.bubuko.com/infodetail-3152998.html