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היא ספריית מתודות קלאס וקבועים מוכנים מראש. אין צורך ב-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()מחזירהdoubleבטווח0.0ומעלה (אך לא כולל)1.0. קנה מידה והזז אותה עבור טווח:(int)(Math.random() * 6)נותן0–5.- הוסף
1לקבלת גלגלת קובייה:(int)(Math.random() * 6) + 1נותן1–6. עבור רוחב שלם חיובי וקטן n שהתוויות הנובעות ממנו יכנסו ב-int,(int)(Math.random() * n) + startנותן n תוויות שלמות אפשריות. עבור 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— בצע המרה לאחר קנה המידה, ולא לפני. הערבוב ביןintלdoubleהוא טעות קלאסית במחלקת Math.
Math.pow ו-Math.sqrt מחזירים double, לא int. Math.pow(2, 3) הוא 8.0; אחסן אותו ב-double, או המר סוג כדי להשתמש בו כ-int. וה-Math.random() מחזיר double ב-[0.0, 1.0); הגבול העליון 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.Math.random() * 6→0.0עד אך לא כולל6.0(כמו2.52).(int)(Math.random() * 6) + 1→ חיתוך ל-2, ואז+1→3.
להעביר את הניתוח למקרה חדש
- המרה לפני קנה המידה,
(int)Math.random() * 6 + 4, תמיד נותנת 4. בדוק u=0 וערך מייצג קצת מתחת ל1 ב-(int)(u * 6) + 4: הקצוות האפשריים הם 4 ו9; u=1 מחוץ לטווח המתודות האקראיות. - מתודות הספרייה גם הן מתנהגות בגבולות:
Math.sqrt(-1)מחזירה NaN, וMath.abs(Integer.MIN_VALUE)מחזירה אותו שלם שלילי זה כי הגודל החיובי שלו אינו מכנס ב-int. הדוגמה הרגילה abs(-5)=5 עדיין תקפה.
The Math class provides static methods: abs, pow, sqrt, max, min, and random. pow/sqrt return a double (cast for an int). Math.random() returns a double in [0.0, 1.0); build an integer range with (int)(Math.random() * n) + start.
מה השתבש? רק צוות האתר רואה זאת.