Javaで2つの数値のmin()を見つけるにはどうすればよいですか?
Java は、広範な便利な操作のために「 java.lang.Math 」として知られるシステム ライブラリを提供します。三角関数から対数関数まで、このライブラリの多様な機能により、このライブラリが提供するメソッドを使用して、数値の最小/最大、さらには絶対値を見つけることができます。Math.min() メソッド
以下はメソッドの通常の表現です。
Math.min(a, b)
この関数は、同じ型int、long、float、またはdouble の 2 つのパラメータを受け入れることに注意してください。Math.min()メソッドの効果的な使用法を理解するために、その実行可能な例を見てみましょう。必ず IDE でスクリプトを実行して出力を検証してください。
例1
package com.math.min.core
public class MathMinMethod {
public static void main(String[] args) {
int leenasAge = 10;
int tiffanysAge = 15;
// example of min () function
int min = Math.min(leenasAge, tiffanysAge);
System.out.print("Who's the younger sister? ");
if (min < tiffanysAge)
System.out.print("Leena ------- Age " + leenasAge);
else
System.out.print("Tiffany ------- Age " + tiffanysAge);
}
}
出力
妹は誰ですか? リーナ ------- 10 歳
説明
8 行目では、int min = Math.min(leenasAge, tiffanysAge); int min には、 min()関数によって返される最小数が格納されます。後でその結果を使用して、小さい兄弟の年齢を見つけます。例 2
package com.math.min.core;
public class MathMinMethod {
public static void main(String[] args) {
double berriesSoldInKg = 15.6;
double cherriesSoldInKg = 21.3;
// example of min () function
double min = Math.min(berriesSoldInKg, cherriesSoldInKg);
System.out.println("What's the minimum weight sold?");
if (min != cherriesSoldInKg)
System.out.print("Berries: " + berriesSoldInKg + " kg");
else
System.out.print("Cherries: " + cherriesSoldInKg +"kg");
}
}
出力
最低販売重量はどれくらいですか?ベリー:15.6kg
GO TO FULL VERSION