예제가 포함된 Java 수학 min() 메서드
그만큼 java.lang.math.min() 함수는 두 숫자 중 최소값을 반환하는 Java의 내장 함수입니다. 인수는 int, double, float 및 long으로 사용됩니다. 음수와 양수가 인수로 전달되면 음수 결과가 생성됩니다. 그리고 전달된 두 매개변수가 모두 음수이면 더 높은 크기의 숫자가 결과로 생성됩니다.
통사론:
dataType min(dataType num1, dataType num2) The datatypes can be int, float, double or long. Parameters : The function accepts two parameters num1 and num2 among which the minimum is returned
반환 값: 이 함수는 두 숫자 중 최소값을 반환합니다. 데이터 유형은 인수의 데이터 유형과 동일합니다.
다음은 min() 함수의 예입니다.
자바
// Java program to demonstrate the> // use of min() function> // two double data-type numbers are passed as argument> public> class> Gfg {> > public> static> void> main(String args[])> > {> > double> a => 12.123> ;> > double> b => 12.456> ;> > // prints the minimum of two numbers> > System.out.println(Math.min(a, b));> > }> }> |
산출:
12.123
자바
// Java program to demonstrate the> // use of min() function> // when one positive and one> // negative integers are passed as argument> public> class> Gfg {> > public> static> void> main(String args[])> > {> > int> a => 23> ;> > int> b = -> 23> ;> > // prints the minimum of two numbers> > System.out.println(Math.min(a, b));> > }> }> |
산출:
-23
자바
// Java program to demonstrate> // the use of min() function> // when two negative integers> // are passed as argument> public> class> Gfg {> > public> static> void> main(String args[])> > {> > int> a = -> 25> ;> > int> b = -> 23> ;> > // prints the minimum of two numbers> > System.out.println(Math.min(a, b));> > }> }> |
산출:
-25
코드에서 최소 두 개의 숫자를 여러 번 찾으려면 전체를 작성하는 것이 지루한 경우가 많습니다. 수학.최소() 매번. 따라서 여기서 더 짧고 시간을 절약할 수 있는 방법은 직접 가져오는 것입니다. java.lang.Math.min 정적으로 사용하고 그냥 사용하십시오 분() 완전한 대신 수학.최소() .
자바
import> static> java.lang.Math.min;> class> GFG {> > public> static> void> main(String[] args)> > {> > int> a => 3> ;> > int> b => 4> ;> > System.out.println(min(a, b));> > }> }> |
산출
3