The Math Class
導入済み
15:00
物体と手法の使用
这里是两个Java程序。第一个缺少分号。第二个将七除以二,并将结果存储为整数。编译器会停止…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| 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クラスは、既成の数学クラスメソッドと定数のライブラリです。インポートもオブジェクト作成も不要で、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)— 2つの値の大きい方 / 小さい方。
乱数
Math.random()は**0.0以上1.0未満**の範囲にあるdoubleを返します。範囲をスケールおよびシフトするには:(int)(Math.random() * 6)は0〜5を与えます。- サイコロの目として
1を追加します:(int)(Math.random() * 6) + 1は1–6を与えます。結果のラベルが int に収まる正の適度な整数幅 n に対して、(int)(Math.random() * n) + startは n 個 possible な整数ラベルを与えます。4 から 9 まで(含む)の場合、n は9 - 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として使用するには、キャスト する必要があります:(int) Math.pow(2, 3)。Math.random()もdoubleです—スケール後にキャストし、前にCastしてはいけません。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以上6.0未満(例:2.52)。(int)(Math.random() * 6) + 1→2に切り捨て、次に+1→3。
の推論を新しいケースへ適用せよ
- Cast before scaling,
(int)Math.random() * 6 + 4, always gives 4. u=0 と 1 直前の表現可能な値を(int)(u * 6) + 4で確認すると、考えられる端点は 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 で作ります。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。