The Math Class
도입됨
15:00
객체와 메소드의 사용
여기에 두 개의 자바 프로그램이 있습니다. 첫 번째는 세미콜론이 빠져 있습니다. 두 번째는 7을 2로 나누어 정수 형태로 결과를 저장합니다. 컴파일러가…를 멈춥니다.
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| integer/ˈɪntɪdʒə/ | A whole number that may be positive, negative or zero. |
| Scaling | |
| truncating | |
| shifting | |
| class/klæs/ | |
| library/ˈlaɪbrəri/ | A collection of reusable code, such as ready-made classes and methods, available for programs to use. |
| class methods | |
| constants | |
| import/ɪmˈpɔːt/ | To load a module or make its names available for use in a program. |
| object/ˈɒbdʒekt/ | An entity in a program with associated state and behaviour, often an instance of a class. |
| powers | |
| roots/ruːts/ | |
| rounding/ˈraʊndɪŋ/ | |
| absolute value/ˈæbsəluːt ˈvæljuː/ | |
| input/ˈɪnpʊt/ | A value, signal or resource supplied to a function, program, device or process. |
| output type | |
| square root/skweə ruːt/ | |
| upper bound/ˈʌpə baʊnd/ | A value that is greater than or equal to every value in the set being considered. |
| double/ˈdʌbl/ | |
| boundary behavior | |
| NaN | |
| static methods |
이 카드는 새로 추가되어 번역이 준비될 때까지 영어로 표시됩니다.
Trace a random range at both ends
- Tickets need integer 整数 labels from 4 through 9 inclusive. Use (int)(Math.random() * 6) + 4.
- Scaling 缩放 gives values from 0 up to but not including 6; truncating 截断 gives 0 through 5; shifting 平移 gives 4 through 9.

Math 툴박스
Math클래스는 사전 제작된 수학 클래스 메서드와 상수를 포함하는 라이브러리입니다. import나 객체가 필요 없이Math.something(...)만 호출하면 됩니다.- 지수, 루트, 반올림, 절대값, 최소/최대 등을 다룹니다. 작업에 맞는 입력, 출력 유형 및 수학적 의미를 가진 메서드를 선택하십시오. 모든 입력이 유한한 결과를 내지는 않습니다.
AP용 일반 메서드
Math.abs(x)— 절대값:Math.abs(-5)은5입니다.Math.pow(b, e)—b의e승:Math.pow(2, 3)은8.0입니다.Math.sqrt(x)— 제곱근:Math.sqrt(9)은3.0입니다.Math.max(a, b)/Math.min(a, b)— 두 값 중 더 큰 / 더 작은 값
무작위 숫자
Math.random()은0.0부터 (포함하지 않고)1.0까지 범위의double을 반환합니다. 범위 확장 및 이동:(int)(Math.random() * 6)은0–5범위를 제공합니다.- Add
1for a die roll:(int)(Math.random() * 6) + 1gives1–6. For a positive, modest integer width n whose resulting labels fit in int,(int)(Math.random() * n) + startgives n possible integer labels. For 4 through 9 inclusive, n is9 - 4 + 1 = 6.
Building a die roll from Math.random()
Scale, truncate, then shift to get 1-6.
What does Math.pow(2, 3) equal (as a number)?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
2 to the power 3 = 8 (returned as the double 8.0).
Math.sqrt(9) returns a value of which type?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
sqrt always returns a double — cast to use it as an int.
Math.random() returns a double in which range?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
[0.0, 1.0) — the upper bound 1.0 is never returned.
Which expression gives a random integer from 1 to 6?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Scale by 6 → 0..5, truncate, then +1 → 1..6.
What is Math.abs(-5)?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Absolute value of −5 is 5.
반환 유형 주의
Math.pow과Math.sqrt은 항상double(예:8.0,8가 아님)을 반환합니다. 결과를int로 사용하기 위해서는 형 변환(cast) 을 반드시 수행해야 합니다:(int) Math.pow(2, 3).Math.random()도 **double**입니다 — 스케일링 후, 전에는 아닌 나중에 강제 변환해야 합니다. **int**과 **double**를 혼동하는 것은 Math 클래스에서 자주 발생하는 실수입니다.
Math.pow와 Math.sqrt은 double를 반환하며, int가 아닙니다. Math.pow(2, 3)은 8.0입니다. 이를 double에 저장하거나, 변환하여 int로 사용하십시오. 또한 Math.random()은 [0.0, 1.0) 범위 내에서 double를 반환하며, 상한선 1.0은 절대 반환되지 않습니다. 정수 범위를 만들려면 (int)(Math.random() * n) + start을 사용하십시오.
Order the operations that build labels 4 through 9.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Scaling must happen before the cast; otherwise every initial cast is zero.
For labels 4 through 9, substitute u=0 into (int)(u * 6) + 4. What label results?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
The scaled and cast value is zero, then 4 is added.
Math.sqrt(-1) throws an exception instead of returning a double result.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
It returns the special double value NaN; this is different from an exception.
무작위 주사위 굴림(1–6):
Math.random()→0.42같은 double 값.Math.random() * 6→0.0이상 but6.0미만 (예:2.52).(int)(Math.random() * 6) + 1→2로 절단, 이후+1→3.
추론을 새로운 사례로 확장하기
- 스케일링 전에 강제 변환하면,
(int)Math.random() * 6 + 4은 항상 4을 반환합니다. u=0와(int)(u * 6) + 4에서 표현 가능한 1 바로 아래 값을 확인하세요; 가능한 끝점은 4과 9이며, u=1은 무작위 방법의 범위 바깥에 있습니다. - 라이브러리 메소드에도 경계 행위가 있습니다:
Math.sqrt(-1)은 NaN을 반환하고,Math.abs(Integer.MIN_VALUE)은 양의 절댓값이 int에 들어가지 않아 같은 음수 int를 반환합니다. 일반적인 예 abs(-5)=5는 여전히 성립합니다.
Math 클래스는 정적 메서드를 제공합니다: abs, pow, sqrt, max, min, random. pow/sqrt은 **double**을 반환하므로(int으로 사용 시 캐스팅 필요), Math.random()은 [0.0, 1.0) 범위 내에서 **double**을 반환합니다. 정수 범위는 (int)(Math.random() * n) + start으로 만듭니다.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.