输入三个整数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] + "\t");
    }        
    }
}

方法二:

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);
            }
        }
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM