public class Ex08_MathClass {
public static void main(String[] args) {
int a = 5, b = -3;
// 두 값 중 큰 값
System.out.println("max : " + Math.max(a, b)); // 5
// 두 값 중 작은 값
System.out.println("min : " + Math.min(a, b)); // -3
// 절댓값
System.out.println("abs : " + Math.abs(b)); // 3
// 제곱
System.out.println("pow : " + Math.pow(2, 3)); // 2^3 = 8.0
// 제곱근
System.out.println("sqrt : " + Math.sqrt(16)); // 4.0
// 랜덤 값 (0.0 이상 ~ 1.0 미만)
System.out.println("random : " + Math.random());
// 반올림 / 올림 / 내림
System.out.println("round : " + Math.round(3.6)); // 4
System.out.println("ceil : " + Math.ceil(3.1)); // 4.0
System.out.println("floor : " + Math.floor(3.9)); // 3.0
}
}
📌 주요 메서드 정리
- Math.max(a, b) → 두 값 중 큰 값
- Math.min(a, b) → 두 값 중 작은 값
- Math.abs(x) → 절댓값
- Math.pow(x, y) → xyx^y (x의 y제곱)
- Math.sqrt(x) → 제곱근
- Math.random() → 0.0 이상 1.0 미만의 난수
- Math.round(x) → 반올림
- Math.ceil(x) → 올림
- Math.floor(x) → 내림
https://docs.oracle.com/javase/8/docs/api/
Java Platform SE 8
docs.oracle.com
'기초 및 언어 > ▶ Java&JSP' 카테고리의 다른 글
| 16. setter/ getter (2) | 2025.08.18 |
|---|---|
| 15. 오버로딩 (1) | 2025.08.18 |
| 13. 재귀호출 (3) | 2025.08.18 |
| 12. 변수 (1) | 2025.08.18 |
| 11. 배열넘기기 (0) | 2025.08.18 |