加载中...

JavaSE常用类之Math类

Math类

数学类的方法都是静态方法,可以直接引用——Math.方法();

常用数学类方法:

  1. E 常量:e
  2. PI 常量:π
  3. max():求最大值
  4. min():求最小值
  5. abs():获取绝对值
  6. pow():求次幂
  7. round():四舍五入
  8. sqrt():求平方根
  9. cbrt():求立方根
  10. ceil():返回大于或等于 a 的最小整数
  11. floor():返回小于或等于 a 的最大整数

代码:

package MathKind;
//导入Math包
import  java.lang.Math;
//import static java.lang.Math.max;//静态调用
public class MathK {
        public static void main(String args[]){
            //求e(自然对数)和 π(圆周率)
            System.out.println(Math.E);            //e=2.718281828459045
            System.out.println(Math.PI);           //π=3.141592653589793
            //求最大值、最小值、绝对值
            System.out.println(Math.max(8,22));    //22
            System.out.println(Math.min(8,22));    //8
            System.out.println(Math.abs(-18));     //18
            //求次幂
            System.out.println(Math.pow(5,3));     //125.0
            //四舍五入
            System.out.println(Math.round(8.7));   //9
            System.out.println(Math.round(8.3));   //8
            //求平方根
            System.out.println(Math.sqrt(9));      //3.0
            //求立方根
            System.out.println(Math.cbrt(8));      //2.0
            //返回大于或等于 a 的最小整数
            System.out.println(Math.ceil(8.6));    //9.0
            //返回小于或等于 a 的最大整数
            System.out.println(Math.floor(7.8));   //7.0
        }
    }
posted @ 2022-04-05 17:12  别捏小祎脸了  阅读(51)  评论(0)    收藏  举报