JAVA函數問題

設計一個函式,傳入3個整數數字,然後傳回大小居於中間的數字

public class qq

{

public static void main(String [] args)

{

}

public static int sum( int x, int y,int z)

{

}

}

上面是我想大概的架構,如果要顯示出3數中,中間數要怎用阿?

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    請參考我的做法

    public static int middle(int x, int y, int z) {

    int a = x - z;

    int b = y - z;

    if (a >= 0 && b >= 0) {

    return (a >= b) ? y : x;

    } else if (a < 0 && b < 0) {

    return (a >= b) ? x : y;

    } else {

    return z;

    }

    }

Still have questions? Get your answers by asking now.