Java에서 두 숫자의 min()을 찾는 방법은 무엇입니까?
Java는 광범위하고 편리한 작업을 위해 " java.lang.Math " 로 알려진 시스템 라이브러리를 제공합니다 . 삼각법에서 로그 함수에 이르기까지 다양한 기능으로 인해 이 라이브러리에서 제공하는 방법을 사용하여 숫자의 최소/최대 또는 절대값을 찾을 수 있습니다.Math.min() 메서드
다음은 메서드의 일반적인 표현입니다.Math.min(a, b)
이 함수는 동일한 유형 int , long , float 또는 double 의 두 매개변수를 허용합니다 . 효과적인 사용법을 이해하기 위해 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