← 從零開始學 Java
第 11 課,共 88 課 · 10 XP
Math
理論與題目說明
機器翻譯,尚未經母語人士審閱。
Math.max(5, 10) // 10
Math.min(5, 10) // 5
Math.abs(-4.7) // 4.7
Math.sqrt(64) // 8.0
Math.pow(2, 10) // 1024.0——double
Math.round(2.5) // 3——取最接近的值,一半時往上(C# 是 2)
Math.floor(2.9) // 2.0
Math.ceil(2.1) // 3.0
Math.PI // 3.141592653589793
Math.random() // 亂數 0.0 ≤ x < 1.0
Math.floorMod(-7, 3) // 2——「數學的」餘數,和 -7 % 3 == -1 不同
1 到 6 的整數亂數:(int) (Math.random() * 6) + 1。要在測試裡能重現——用 new Random(seed)。
題目。 讀入兩點的座標 x1 y1 x2 y2(整數)。以小數點後 2 位(%.2f)輸出兩點間的距離,
第二行輸出用 Math.round 四捨五入後的值。