zoukankan      html  css  js  c++  java
  • 输入三个整数x,y,z,请把这三个数据由大到小输出。

    针对这个题目,我就简单的使用两个方法进行排序:

    方法一:

    public class TreeOfSort{
        public static void main(String [] args){
        //由键盘输入三个数字
        Scanner input = new Scanner(System.in);
        int [] a = new int[3];
        for(int i = 0; i < 3; i++) {
            a[i] = input.nextInt();
        }
        //调用内部函数
        Arrays.sort(a);
            
        for(int j = 0; j<3; j++) {
            System.out.print(a[j] + "	");
        }        
        }
    }

    方法二:

    package studying;
    
    import java.util.Scanner;
    /*
     * This method is not recommended!
     * It is easy to make a mistake by comparing it abruptly.
     */
    
    public class ThreeOfSort2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.println("please enter the first number:");
            int a = input.nextInt();
            System.out.println("please enter the second number:");
            int b = input.nextInt();
            System.out.println("please enter the third number:");
            int c = input.nextInt();
            
            if(a > b) {
                if(c > a) {
                    System.out.println(b + "," + a + "," + c);
                }else if(c < b) {
                    System.out.println(c + "," + b + "," + a);
                }else {
                    System.out.println(b + "," + c + "," + a);
                }
                
            }else {//a < b
                if(c < a) {
                    System.out.println(c + "," + a + "," + b);
                }else if(c > b) {
                    System.out.println(a + "," + b + "," + c);
                }else {
                    System.out.println(a + "," + c + "," + b);
                }
            }
        }
    
    }
  • 相关阅读:
    Java实现各种排序算法
    Memcached学习笔记
    S.O.L.I.D 原则
    设计模式之Bridge
    UML建模工具比较
    UML建模
    Ps经典实例教程3000例
    ps视频教程全集
    自己做到了吗?
    记事本开发Java代码注意要点
  • 原文地址:https://www.cnblogs.com/superdrew/p/8067240.html
Copyright © 2011-2022 走看看