15.7 수학 클래스 (Math)
1. 공학용 계산기 🧮
Math 클래스는 수학 계산을 돕는 도구 모음입니다.
모두 정적(static) 메소드이므로 객체 생성 없이 바로 씁니다.
| 기능 | 메소드 | 예시 | 결과 |
|---|---|---|---|
| 절대값 | abs |
Math.abs(-5) |
5 |
| 올림 | ceil |
Math.ceil(5.3) |
6.0 |
| 버림 | floor |
Math.floor(5.7) |
5.0 |
| 반올림 | round |
Math.round(5.3) |
5 |
| 최대값 | max |
Math.max(5, 10) |
10 |
| 최소값 | min |
Math.min(5, 10) |
5 |
2. 주사위 던지기 (random) 🎲
Math.random()은 0.0 이상 1.0 미만의 난수(Random Number)를 줍니다.
이걸 잘 조절하면 주사위나 로또 번호를 만들 수 있습니다.
공식: (int) (Math.random() * 개수) + 시작숫자
// 1부터 시작하는 6개의 정수 (주사위)
int num = (int) (Math.random() * 6) + 1;
System.out.println("주사위 눈: " + num);
3. 전문 난수 생성기 (Random 클래스)
Math.random()보다 더 다양한 기능을 원하면 java.util.Random 클래스를 씁니다.
import java.util.Random;
Random random = new Random();
boolean b = random.nextBoolean(); // true/false
int i = random.nextInt(10); // 0 ~ 9 사이 정수
double d = random.nextDouble(); // 0.0 ~ 1.0 사이 실수
핵심: 복잡한 수학 공식은
Math클래스에게 맡기세요.
서브목차