码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习:Math类

时间:2017-04-27 12:42:38      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:创建   nbsp   随机   取出   arrays   []   style   math类   src   

Math:用于执行数学运算的类。

成员方法:
public static int abs(int a)//绝对值
public static double ceil(double a)//想上去取整
public static double floor(double a)//向下取整
public static int max(int a,int b) //取最大值
public static double pow(double a,double b) //a的b次方
public static double random()//
public static int round(float a) 参数为double的自学

技术分享
 1                 //public static int abs(int a),取绝对值
 2         System.out.println(Math.abs(100));
 3         System.out.println(Math.abs(-100));
 4         
 5         //public static double ceil(double a),向上取整
 6         System.out.println(Math.ceil(12.3));
 7         System.out.println(Math.ceil(12.7));
 8         System.out.println(Math.ceil(12.0));
 9         
10         //public static double floor(double a),向下取整
11         System.out.println(Math.floor(12.3));
12         System.out.println(Math.floor(12.7));
13         System.out.println(Math.floor(12.0));
14         
15         // public static int max(int a,int b) min自学
16         System.out.println(Math.max(2, 3));
17         //判断2,3,4的最大值
18         System.out.println(Math.max(Math.max(2, 3), 4));
19         //如果是多个数字求最大值:
20         //1.创建数组,将数据存储到数组中,接着使用for+if取出最大值
21         //2.冒泡排序,取出最后一个值
22         //3.Arrays.sort(int[] arr),取出最后一个
23         
24         //public static double pow(double a,double b)  a的b次方
25         System.out.println(Math.pow(2, 3));
26         
27         System.out.println("---------------");
28         
29         //public static double random(),产生的随机数在0-1之间,包括0包括1
30         System.out.println(Math.random());
31         //需求:产生1-100之间的随机数
32         int radom = (int) (Math.random()*100+1);
33         System.out.println(radom);
34         
35         // public static int round(float a) ,四舍五入
36         System.out.println(Math.round(12.4));
37         System.out.println(Math.round(12.6));
成员方法演示

运行结果如下:

100
100
13.0
13.0
12.0
12.0
12.0
12.0
3
4
8.0
---------------
0.7210652393976311
75
12
13

 

Java学习:Math类

标签:创建   nbsp   随机   取出   arrays   []   style   math类   src   

原文地址:http://www.cnblogs.com/shaofanglazi/p/6773495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!