jeffsui / jeffsui.github.io

pinghailinfeng's blog main website
http://jeffsui.github.io
7 stars 5 forks source link

java输入三个数从大到小排列 #165

Open jeffsui opened 2 years ago

jeffsui commented 2 years ago

        int i = 0;
        int j = 0;
        int k = 0;
        int x = 0;
        System.out.print("请输入三个数\n");
        Scanner input = new Scanner(System.in);
        i = input.nextInt();
        j = input.nextInt();
        k = input.nextInt();
        if (i > j) {
            x = i;
            i = j;
            j = x;
        }
        if (i > k) {
            x = i;
            i = k;
            k = x;
        }
        if (j > k) {
            x = j;
            j = k;
            k = x;
        }
        System.out.println("由小到大输出:"+i + ", " + j + ", " + k);
        System.out.println("由大到小输出:"+k + ", " + j + ", " + i);

另一种写法

public class Test04 {
    public static void main(String[] args) {
        demo01();
    }
/*7.题目:输入三个整数x,y,z,请把这三个数由小到大输出。*/
    public static void demo01(){
        System.out.println("欢迎来到排序小游戏!");

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入第一个数x:");
        int x = scanner.nextInt();
        System.out.print("请输入第二个数y:");
        int y=scanner.nextInt();
        System.out.print("请输入第三个数z:");
        int z=scanner.nextInt();
        if(x>y) { 
            if(x>z){ 
                if(y>z){
                    System.out.println(z+"<"+y+"<"+x);
                    return;
                }else{
                    System.out.println(y+"<"+z+"<"+x);
                    return;
                }
            }else{
                System.out.println(y+"<"+x+"<"+z);
                return;
            }
        }
        if(x>z){
            if(z>y){
                System.out.println(y+"<"+z+"<"+x);
                return;
            }else{
                if(x>y){
                    System.out.println(z+"<"+y+"<"+x);
                    return;
                }else{
                    System.out.println(z+"<"+x+"<"+y);
                    return;
                }
            }
        }
        if(y>z){
            if(z>x){
                System.out.println(x+"<"+z+"<"+y);
                return;
            }else{
                if(x>y){
                    System.out.println(z+"<"+y+"<"+x);
                    return;
                }else {
                    System.out.println(z+"<"+x+"<"+y);
                    return;
                }
            }
        }
    }
 }