java.lang.Math类,方法学习笔记

 1 /**java.lang 包中的Math 类提供有常量

 2  * 并包含有用于执行基本数学运算的

 3  * 方法,如初等指数、对数、平方根

 4  * 用于进行更高级的数学运算。由于

 5  * 在Math 类的方法都是静态的,因此可

 6  * 直接通过类来调用

 7  * Math.cos(angle);

 8  * (使用静态导入(static import)的语言特性"import static java.lang.Math.*"

 9  * 就不必在每一个数学函数前面都写Math 了。这允许通过简单的

10  * 名称调用Math 类中的方法,例如:“cos(sngle)”!

11  */ 

12 public class Hello { 

13     public static void main(String[] args) { 

14         /**1.常量和基本方法

15          * Math 包含两种常量

16          * 1>Math.E:代表自然对数的基数,double 类型

17          * 2>Math.PI:圆周率,double 类型

18          * Math 类包含超过40 个静态方法

19          */ 

20         double a=-191.635; 

21         double b=41.94; 

22         int c=27,d=65; 

23         System.out.printf("%.3f 的绝对值是:%.3f%n",a,Math.abs(a)); 

24         System.out.printf("比%.2f 大的最小整数 是%.0f%n",b,Math.ceil(b)); 

25         System.out.printf("比%.2f 小的最大整数 是%.0f%n",b,Math.floor(b)); 

26         /**指数和对数方法

27          * exp 自然对数的e 次幂

28          * log 参数的自然对数

29          * pow 第一个参数的第二个参数的次幂

30          * sprt 返回参数的平方根

31          */ 

32         double x=12.715; 

33         double y=3.96; 

34         System.out.printf("自然地数的基数是:%.4f%n", Math.E); 

35         System.out.printf("exp(%.3f)是%.3f%n",x,Math.exp(x)); 

36         System.out.printf("log(%.3f)是%.3f%n",x,Math.log(x)); 

37         System.out.printf("sqrt(%.3f)是%.3f%n",x,Math.sqrt(x)); 

38         /**随机数

39          * Math 类的random()静态方法返回在0.0 和1.0 之间的一个随机数

40          * 区间包括0.0 但不包括1.0

41          * 假如要生成一个0-9 的整数

42          * 可以这样int number=(int)(Math.random()*10)

43          */ 

44         System.out.print((int)(Math.random()*10)); 

45     } 

46 }

 

你可能感兴趣的:(java)