方法 | 描述 |
---|---|
double abs(double d) float abs(float f) int abs(int i) long abs(long lng) | 回傳參數的絕對值 |
舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class AbsDemo { public static void main(String[] args) { int a1 = - 23 ; long a2 = 96 ; float a3 = - 63 .25f; double a4 = 9.07 ; System.out.println(Math.abs(a1)); System.out.println(Math.abs(a2)); System.out.println(Math.abs(a3)); System.out.println(Math.abs(a4)); } } /* 《程式語言教學誌》的範例程式 檔名:AbsDemo.java 功能:示範物件導向的基本觀念 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

其中,第 8 行到第 11 行
8 9 10 11 | System.out.println(Math.abs(a1)); System.out.println(Math.abs(a2)); System.out.println(Math.abs(a3)); System.out.println(Math.abs(a4)); |
這裡直接呼叫 abs() 方法,印出回傳的絕對值。
中英文術語對照 | |
---|---|
方法 | method |
參數 | parameter |
沒有留言:
張貼留言