前言:
 不要去听别人的忽悠,你人生的每一步都必须靠自己的能力完成。自己肚子里没有料,手上没本事,认识再多人也没用 。   
我们上一篇聊到了java之System类,这一篇我们聊一下Math类类,针对于以下的概念,都会有实体例子配合着,给大家演示,希望给历险中的你带来一些帮助!!!

一.介绍
Java的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。
Math类位于Java.lang包中,包含用于执行基本数学运算的方法!Math类的所有执行方法都是静态方法,可以直接使用类名.方法名调用。
Math类是一个唯一(final)类或工具类,他提供了我们所需要的所有的数学方法及属性,比如:我们的笔记本电脑,他就是一个工具,我们拥有了他就可以直接去使用,我们可以使用他里面的office软件、聊天软件、听歌软件等,还有他本身的显示器,键盘,鼠标等。
二.知识点介绍
1、Math类中的常用方法
2、精炼练习
三.知识点详解
1、Math类中的常用方法
(1)public staticfinal Double E = 2.7182818284590452354
(2)public staticfinal Double PI = 3.14159265358979323846
(3)public staticlong abs(double x):传回 x 的绝对值。X也可int long float
(4)public staticlong pow(double x,double y):传回x的y次幂值
(5)public staticlong sqrt(double x): 传回x开平方值
(6)public staticlong max(double x,double y):传回x、y较大数
(7)public staticlong min(double x,double y):传回x、y较小数
(8)public staticlong floor(double x):传回不大于x的最大整数值 ,向下取整
(9)public staticlong ceil(double x):传回不小于x的最小整数值,向上取整
(10)publicstatic long round(double x):传回x的四舍五入值
(11)public static long random():传回随机数值,产生一个0-1之间的随机数(不包括0和1)
(12)publicstatic long exp(double x):传回相当于e^x
(13)publicstatic long log(double x):传回x的自然对数函数值
(14)publicstatic long rint(double x):传回最接近x的整数值
(15)publicstatic long toDegrees(double angrad):传回将angrad径度转换成角度
(16)publicstatic long toRadians(double angdeg): 传回将angdeg角度转换成径度
(17)publicstatic long sin(double x): 传回x径度的正弦函数值
(18)public staticlong cos(double x):传回x径度的余弦函数值
(19)publicstatic long tan(double x): 传回x径度的正切函数值
(20)publicstatic long asin(double x):传回x值的反正弦函数值。
(21)publicstatic long acos(double x):传回x值的反余弦函数值。
(22)publicstatic long atan(double x):传回x值的反正切函数值。
(23)publicstatic long atan2(double x, double y):传回极坐标(polar)的θ值
代码演示:
package com.Test;
import Test2.MyDate;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
public class Main {
    private final static String name = "磊哥的java历险记-@51博客";
        public static void main(String args[]){
            /**
             *Math.sqrt()//计算平方根
             *Math.cbrt()//计算立方根
             *Math.pow(a, b)//计算a的b次方
             *Math.max( , );//计算最大值
             *Math.min( , );//计算最小值
             */
            System.out.println(Math.sqrt(16));   //4.0
            System.out.println(Math.cbrt(8));    //2.0
            System.out.println(Math.pow(3,2));     //9.0
            System.out.println(Math.max(2.3,4.5));//4.5
            System.out.println(Math.min(2.3,4.5));//2.3
            /**
             * abs求绝对值
             */
            System.out.println(Math.abs(-10.4));   //10.4
            System.out.println(Math.abs(10.1));    //10.1
            /**
             * ceil天花板的意思,就是返回大的值
             */
            System.out.println(Math.ceil(-10.1));  //-10.0
            System.out.println(Math.ceil(10.7));   //11.0
            System.out.println(Math.ceil(-0.7));   //-0.0
            System.out.println(Math.ceil(0.0));    //0.0
            System.out.println(Math.ceil(-0.0));    //-0.0
            System.out.println(Math.ceil(-1.7));   //-1.0
            /**
             * floor地板的意思,就是返回小的值
             */
            System.out.println(Math.floor(-10.1)); //-11.0
            System.out.println(Math.floor(10.7));   //10.0
            System.out.println(Math.floor(-0.7));  //-1.0
            System.out.println(Math.floor(0.0));   //0.0
            System.out.println(Math.floor(-0.0));  //-0.0
            /**
             * random 取得一个大于或者等于0.0小于不等于1.0的随机数
             */
            System.out.println(Math.random());  //小于1大于0的double类型的数
            System.out.println(Math.random()*2);//大于0小于1的double类型的数
            System.out.println(Math.random()*2+1);//大于1小于2的double类型的数
            /**
             * rint 四舍五入,返回double值
             * 注意.5的时候会取偶数    异常的尴尬=。=
             */
            System.out.println(Math.rint(10.1));   //10.0
            System.out.println(Math.rint(10.7));   //11.0
            System.out.println(Math.rint(11.5));   //12.0
            System.out.println(Math.rint(10.5));    //10.0
            System.out.println(Math.rint(10.51));  //11.0
            System.out.println(Math.rint(-10.5));  //-10.0
            System.out.println(Math.rint(-11.5));  //-12.0
            System.out.println(Math.rint(-10.51)); //-11.0
            System.out.println(Math.rint(-10.6));   //-11.0
            System.out.println(Math.rint(-10.2));  //-10.0
            /**
             * round 四舍五入,float时返回int值,double时返回long值
             */
            System.out.println(Math.round(10.1));  //10
            System.out.println(Math.round(10.7));  //11
            System.out.println(Math.round(10.5));  //11
            System.out.println(Math.round(10.51));  //11
            System.out.println(Math.round(-10.5)); //-10
            System.out.println(Math.round(-10.51));//-11
            System.out.println(Math.round(-10.6)); //-11
            System.out.println(Math.round(-10.2)); //-10
            System.out.println("============="+name+"=============");
        }
    }

代码演示二:
package com.Test;
import Test2.MyDate;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
public class Main {
    private final static String name = "磊哥的java历险记-@51博客";
        public static void main(String args[]){
                    // TODO Auto-generatedmethod stub
                    System.out.println(Math.E);//比任何其他值都更接近 e(即自然对数的底数)的 double 值。
                    System.out.println(Math.PI);//比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
                    /*
                     * 1.abs绝对值函数
                     * 对各种数据类型求绝对值
                     */
                    System.out.println(Math.abs(-10));//输出10
                    /*
                     * 2.三角函数与反三角函数
                     * cos求余弦
                     * sin求正弦
                     * tan求正切
                     * acos求反余弦
                     * asin求反正弦
                     * atan求反正切
                     * atan2(y,x)求向量(x,y)与x轴夹角
                     */
                    System.out.println(Math.acos(-1.0));//输出圆周率3.14...
                    System.out.println(Math.atan2(1.0, 1.0));//输出 π/4 的小数值
                    /*
                     * 3.开根号
                     * cbrt(x)开立方
                     * sqrt(x)开平方
                     * hypot(x,y)求sqrt(x*x+y*y)在求两点间距离时有用sqrt((x1-x2)^2+(y1-y2)^2)
                     */
                    System.out.println(Math.sqrt(4.0));//输出2.0
                    System.out.println(Math.cbrt(8.0));//输出2.0
                    System.out.println(Math.hypot(3.0, 4.0));//输出5.0
                    /*
                     * 4.最值
                     * max(a,b)求最大值
                     * min(a,b)求最小值
                     */
                    System.out.println(Math.max(1, 2));//输出2
                    System.out.println(Math.min(1.9, -0.2));//输出-0.2
                    /*
                     * 5.对数
                     * log(a) a的自然对数(底数是e)
                     * log10(a) a 的底数为10的对数
                     * log1p(a) a+1的自然对数
                     * 值得注意的是,前面其他函数都有重载,对数运算的函数只能传double型数据并返回double型数据
                     */
                    System.out.println(Math.log(Math.E));//输出1.0
                    System.out.println(Math.log10(10));//输出1.0
                    System.out.println(Math.log1p(Math.E-1.0));//输出1.0
                    /*
                     * 6.幂
                     * exp(x) 返回e^x的值
                     * expm1(x) 返回e^x - 1的值
                     * pow(x,y) 返回x^y的值
                     * 这里可用的数据类型也只有double型
                     */
                    System.out.println(Math.exp(2));//输出E^2的值
                    System.out.println(Math.pow(2.0, 3.0));//输出8.0
                    /*
                     * 7.随机数
                     * random()返回[0.0,1.0)之间的double值
                     * 这个产生的随机数其实可以通过*x控制
                     * 比如(int)(random*100)后可以得到[0,100)之间的整数
                     */
                    System.out.println((int)(Math.random()*100));//输出[0,100)间的随机数
                    /*
                     * 8.转换
                     * toDegrees(a) 弧度换角度
                     * toRadians(a) 角度换弧度
                     */
                    System.out.println(Math.toDegrees(Math.PI));//输出180.0
                    System.out.println(Math.toRadians(180));//输出 π 的值
                    /*
                     * 9.其他
                     */
                    //copySign(x,y) 返回 用y的符号取代x的符号后新的x值
                    System.out.println(Math.copySign(-1.0,2.0));//输出1.0
                    System.out.println(Math.copySign(2.0, -1.0));//输出-2.0
                    //ceil(a) 返回大于a的第一个整数所对应的浮点数(值是整的,类型是浮点型)
                    //可以通过强制转换将类型换成整型
                    System.out.println(Math.ceil(1.3443));//输出2.0
                    System.out.println((int)Math.ceil(1.3443));//输出2
                    //floor(a) 返回小于a的第一个整数所对应的浮点数(值是整的,类型是浮点型)
                    System.out.println(Math.floor(1.3443));//输出1.0
                    //rint(a) 返回最接近a的整数的double值
                    System.out.println(Math.rint(1.2));//输出1.0
                    System.out.println(Math.rint(1.8));//输出2.0
                    //nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小
                    System.out.println(Math.nextAfter(1.2, 2.7));//输出1.2000000000000002
                    System.out.println(Math.nextAfter(1.2,-1));//输出1.1999999999999997
                    //所以这里的b是控制条件
                    //nextUp(a) 返回比a大一点点的浮点数
                    System.out.println(Math.nextUp(1.2));//输出1.2000000000000002
                    //nextDown(a) 返回比a小一点点的浮点数
                    System.out.println(Math.nextDown(1.2));//输出1.1999999999999997
                    System.out.println("============="+name+"=============");
                }
            }


2 精炼练习
在Math类使用的过程当中,我们的随机数是一个比较常用的方法,下面我们的案例就是针对随机数的一个案例。
2.1 题目
(1)随机生成100个随机数,随机数的值必须是100以内的值。
(2)找出100个随机数中,最大的数与最小的数
(3)统计出100个随机数中大于50的数的个数
2.2 实验步骤
(1)声明一个类Test
(2)在Test类中,完成随机数的获取,随机数的比较,以及随机数的统计
代码示例:
public class Test { 
   public staticvoid main(String[] args) {
      int min = 99;int max = 0; int temp; int count = 0;
      for (int i =0; i < 100; i++) { 
          temp =(int) (Math.random() * 100); 
          System.out.print (temp+","); 
          if (temp> 50) { 
             count++;
             } 
          if (min> temp) { 
             min =temp; 
             } 
          if (max< temp) {
             max =temp;
             } 
          } 
      System.out.println();
      System.out.println("Max is:" + max); 
      System.out.println("Min is:" + min);
      System.out.println("The count that biggerthan 50 is:" + count); 
      } 
   }

结语:
不为失败找理由,要为成功找方法。决定今天的不是今天,而是昨天对人生的态度;决定明天的不是明天,而是今天对事业的作为。我们的今天由过去决定,我们的明天由今天决定!
我会沿着java基础至中阶,高阶,和后面的实战项目,循序渐进,由浅入深,希望能够帮助看到我文章的兄弟们,特别是初学者!感谢大家持续关注!

