2020/03/23 公開
・最小値・最大値
ここでの最小値は、与えられた2つの値のうち小さい方の値のことです。最大値は、与えられた2つの値のうち大きい方の値のことです。
最小値 Math.minメソッド
public static int Math.min( int a, int b )
public static long Math.min( long a, long b )
public static float Math.min( float a, float b )
public static double Math.min( double a, double b )
public static int Math.min( byte a, byte b )
public static int Math.min( short a, short b )
public static long Math.min( long a, long b )
public static float Math.min( float a, float b )
public static double Math.min( double a, double b )
public static int Math.min( byte a, byte b )
public static int Math.min( short a, short b )
■引数a,bで指定した数値の小さい方を返します。 パラメータ a, b : 比較する2つの数値 戻り値 aとbを比較して、小さい値を返します。 戻りの変数型は、基本的に引数と同じ変数型と考えてよいです。 ただし、引数の変数型がbyteとshortの場合は、変数型intです。
最大値 Math.maxメソッド
public static int Math.max( int a, int b )
public static long Math.max( long a, long b )
public static float Math.max( float a, float b )
public static double Math.max( double a, double b )
public static int Math.max( byte a, byte b )
public static int Math.max( short a, short b )
public static long Math.max( long a, long b )
public static float Math.max( float a, float b )
public static double Math.max( double a, double b )
public static int Math.max( byte a, byte b )
public static int Math.max( short a, short b )
■引数a,bで指定した数値の大きい方を返します。 パラメータ a, b : 比較する2つの数値 戻り値 aとbを比較して、大きい値を返します。 戻りの変数型は、基本的に引数と同じ変数型と考えてよいです。 ただし、引数の変数型がbyteとshortの場合は、変数型intです。
下記のMinMax.javaは、最小値・最大値を使ったJavaプログラムの例です。
MinMax.java ← クリックしてダウンロードページに移動001: public class MinMax { 002: public static void main( String[] args ) { 003: int a, b, min, max; 004: 005: a = 60; 006: b = 43; 007: 008: // 最小値を変数minに代入 009: min = Math.min( a, b ); 010: 011: // 最大値を変数maxに代入 012: max = Math.max( a, b ); 013: 014: // 結果を表示 015: System.out.println( "最小値=" + min ); 016: System.out.println( "最大値=" + max ); 017: } 018: }
Min.javaの出力結果
最小値=43 最大値=60
最小値・最大値のメソッドは、自作することができます。以下はその例です。
: // int型の最小値 public static int min( int a, int b ) { if ( a < b ) return a; return b; } // int型の最大値 public static int max( int a, int b ) { if ( a > b ) return a; return b; } // long型の最小値 public static long min( long a, long b ) { if ( a < b ) return a; return b; } // long型の最大値 public static long max( long a, long b ) { if ( a > b ) return a; return b; } // float型の最小値 public static float min( float a, float b ) { if ( a < b ) return a; return b; } // float型の最大値 public static float max( float a, float b ) { if ( a > b ) return a; return b; } // double型の最小値 public static double min( double a, double b ) { if ( a < b ) return a; return b; } // double型の最大値 public static double max( double a, double b ) { if ( a > b ) return a; return b; } // byte型の最小値 public static int min( byte a, byte b ) { return min( (int)a, (int)b ); } // byte型の最大値 public static int max( byte a, byte b ) { return max( (int)a, (int)b ); } // short型の最小値 public static int min( short a, short b ) { return min( (int)a, (int)b ); } // short型の最大値 public static int max( short a, short b ) { return max( (int)a, (int)b ); }
(int)aと(int)bで、変数aとbの値をintに変換しています。これをキャストといいます。
この変換によりpublic static int min( int a, int b )、public static int max( int a, int b )が呼ばれます。
■関連コンテンツ
複数の値の最小値と最大値 | 配列から最小値、最大値を取得 |
■新着情報
2021.12.21 | 現在の日時を取得 | いまの年月日、時分秒? |
■広告
