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คลาสคือไลบรารีของ class methods และคงค่าทางคณิตศาสตร์ที่พร้อมใช้งาน ไม่ต้อง import และไม่ต้องสร้าง object — เพียงแค่เรียกMath.something(...)- ครอบคลุมเรื่องกำลังราก การปัดเศษ ค่าสัมบูรณ์ ต่ำสุด/สูงสุด และอื่นๆ เลือก method ที่มีอินพุต ประเภทผลลัพธ์ และความหมายทางคณิตศาสตร์ตรงกับโจทย์; ไม่ใช่ทุกอินพุตจะมีผลเป็นจำนวนจริงจำกัดตามปกติ
เมทโธดที่พบบ่อยสำหรับ AP
Math.abs(x)— ค่าสัมบูรณ์:Math.abs(-5)คือ5Math.pow(b, e)—bยกกำลังe:Math.pow(2, 3)คือ8.0Math.sqrt(x)— รากที่สอง:Math.sqrt(9)คือ3.0Math.max(a, b)/Math.min(a, b)— ค่าที่ใหญ่กว่า / ค่าน้อยกว่าของสองค่า
ตัวเลขสุ่ม
Math.random()คืนค่าdoubleในช่วง0.0ขึ้นไปแต่ไม่รวม1.0ให้ปรับสเกลและเลื่อนตำแหน่งเพื่อสร้างช่วง:(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เช่นกัน — cast หลังการปรับสเกล ไม่ใช่ก่อน สลับintกับdoubleเป็นการผิดพลาดคลาสสิกของ Math class
Math.pow และ Math.sqrt คืนค่า double, ไม่ใช่ int. Math.pow(2, 3) คือ 8.0; เก็บไว้ในตัวแปร double, หรือ cast เพื่อใช้เป็น 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()→ เป็น double อย่าง0.42Math.random() * 6→0.0ขึ้นไปจนถึงเกือบถึง6.0(เช่น2.52)(int)(Math.random() * 6) + 1→ ตัดทอนเป็น2แล้ว ++1→3
ขยายเหตุผลไปยังกรณีใหม่
- การ cast ก่อนการปรับสเกล,
(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 มีเมทโธดแบบ static: abs, pow, sqrt, max, min และ random. pow/sqrt คืนค่า double (ต้อง cast เพื่อให้ได้ int). Math.random() คืนค่า double ในช่วง [0.0, 1.0); สร้างช่วงจำนวนเต็มด้วย (int)(Math.random() * n) + start
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้