java從鍵盤輸入三個整數,實現從小到大排序


package study01;

import java.util.Scanner;

public class Sort {
    /**
     * 需求:由鍵盤輸入三個整數分別存入變量a、b、c,對他們進行 排序(使用if-else),並且從小到大輸出
     * 
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("a=");
        int a = sc.nextInt();

        System.out.print("b=");
        int b = sc.nextInt();

        System.out.print("c=");
        int c = sc.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);
            }

            
        // a<<b時    
        }else {
            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);
            }
        }

    }

}
輸出結果如下:

a=3
b=11
c=1
1,3,11

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM