Math類的經常使用方法和靜態導入.


1
package Test; 2 /** 3 * 測試Math的經常使用方法和靜態導入 4 * @author 小王同窗 5 * 6 */ 7 import static java.lang.Math.*;//使用靜態導入之後能夠直接使用Math中的常量,不用再使用Math.常量. 8 public class TestMath { 9 public static void main(String[] args) { 10 //測試Math類的經常使用方法 11 //取整相關操做 12 System.out.println(Math.ceil(3.2));//向上取整後返回double 13 System.out.println(ceil(3.2));//使用靜態導入之後 14 System.out.println(Math.floor(3.2));//向下取整返回double 15 System.out.println(floor(3.2));//向下取整返回double 16 System.out.println(Math.round(3.2));//四捨五入 17 System.out.println(round(3.2));//四捨五入 18 System.out.println(Math.round(3.8));//四捨五入 19 //絕對值、開方、a的b次冪等操做 20 System.out.println(Math.abs(-45));//求絕對值 21 System.out.println(Math.sqrt(64));//64開平方 22 System.out.println(Math.pow(5, 2));//5的平方 23 System.out.println(Math.pow(2, 5));//2的5次方 24 //Math類中經常使用的常量 25 System.out.println(Math.PI); 26 System.out.println(Math.E); 27 //隨機數 28 System.out.println(Math.random());// 獲得[0,1)的隨機數. 29 } 30 31 }

今天是參加北京尚學堂卓越班的第6天加油!java

相關文章
相關標籤/搜索