Java學習日誌14.11(第一階段基礎)
2018.10.24 下午陰
14.11_常見物件(Math類概述和方法使用)
學習知識:
A:Math類概述
* Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根 和 三角函式。
B:成員方法
* public static int abs(int a)
* //取絕對值,返回int型別
* public static double ceil(double a)
* //天花板,向上取整,返回一個double型別
* public static double floor(double a)
* //地板,向下取整,返回一個double型別
* public static int max(int a,int b) min
* //返回兩個數最大值,可以為int,long.float.double型別
* public static double pow(double a,double b)
* //前數為底,後數為指數,返回一個double型別數
* public static double random()
*
* public static int round(float a) double
* //四捨五入,資料型別可以為double或者float
* public static double sqrt(double a)
* //取平方根,引數型別double
程式碼練習:
package com.heima.Math; public class demo1 { public static void main(String[] args) { //輸出圓周率 System.out.println(Math.PI); System.out.println("_____"); //取絕對值,返回int型別 System.out.println(Math.abs(-10)); System.out.println(Math.abs(10)); System.out.println("_____"); //天花板,向上取整,返回一個double型別 System.out.println(Math.ceil(12.3)); System.out.println(Math.ceil(11.99)); System.out.println("_____"); //地板,向下取整,返回一個double型別 System.out.println(Math.ceil(12.3)); System.out.println(Math.ceil(11.99)); System.out.println("_____"); //返回兩個數最大值,可以為int,long.float.double型別 System.out.println(Math.max(10, 20)); System.out.println(Math.max(1.5, 4.5)); System.out.println(Math.max(7.8f, 4.5f)); System.out.println(Math.max(720L, 45L)); System.out.println("_____"); //返回兩個數最小值,可以為int,long.float.double型別 System.out.println(Math.max(10, 20)); System.out.println(Math.max(1.5, 4.5)); System.out.println(Math.max(7.8f, 4.5f)); System.out.println(Math.max(720L, 45L)); System.out.println("_____"); //前數為底,後數為指數,返回一個double型別數 System.out.println(Math.pow(2, 3)); System.out.println("_____"); //返回一個0.0-1.0之間的隨機小數,包括0.0,不包括1.0 System.out.println(Math.random()); System.out.println("_____"); //四捨五入,資料型別可以為double或者float System.out.println(Math.round(4.4F)); System.out.println(Math.round(5.8)); //取平方根 System.out.println("_____"); System.out.println(Math.sqrt(64)); System.out.println(Math.sqrt(2)); System.out.println(Math.sqrt(3)); } }
程式結果:
3.141592653589793
10
10
13.0
12.0
13.0
12.0
20
4.5
7.8
720
20
4.5
7.8
720
8.0
0.9621352321896282
4
6
8.0
1.4142135623730951
1.7320508075688772