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天加油!
转载于//www.cnblogs.com/xw1024/p/11181899.html
还没有评论,来说两句吧...