Java-Math类
原创约 670 字大约 2 分钟...
117-Java-Math类.md
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
Math类
- Math类提供了大量用于数学运算的方法
- Math类是final类,因此不能从Math类继承
- Math类中的方法都是static方法,因此不必创建Math类的对象就可以直接使用该类的方法
Math类中的常量
public static final double PI
public static final double E
- Math类中的常用方法
方法 | 含义 |
---|---|
static int abs(int) | 返回参数的绝对值,返回值类型与参数类型相同 |
static double abs(double) | 返回参数的绝对值 |
static double ceil(double) | 返回大于所给参数的最小的整数值 |
static double floor(double) | 返回不大于所给参数的最大的整数值 |
static int max(int a,int b) | 返回两个int值中较大的一个 |
static double max(double,double) | 返回两个double值中较大的一个 |
static int min(int a,int b) | 返回两个int值中较小的一个 |
static double min(double,double) | 返回两个double值中较小的一个 |
static double random( ) | 返回在0.0~1.0之间的随机的double值 |
static int round(double) | 返回同所给值最接近的整数,采用4舍5入法 |
static double sin/cos/tan(double) | 返回给定的弧度值对应的三角函数值 |
static double sqrt(double) | 返回所给值的平方根,若所给值为负数则返回NaN |
示例代码:
package Math类;
import java.util.Random;
public class TestMath类 {
public static void main(String[] args) {
/*
* Math类提供了大量用于数学运算的方法 Math类是final类,因此不能从Math类继承 Math类中的方法都是static方法,
* 因此不必创建Math类的对象就可以直接使用该类的方法
*/
System.out.println(Math.PI);
Double d = -123.756;
System.out.println(Math.round(d));// 123,四舍五入取整数
System.out.println(Math.floor(d));// 123.0,向下取整,不会四舍五入
System.out.println(Math.abs(d));// 取绝对值
// 生成随机数
System.out.println(Math.random());// 0.0-1 不包含1,包含0
// 算法:返回1-36之间的整数
int a = (int) (Math.floor(Math.random() * 36)) + 1;
//System.out.println(a);
for (int i = 0; i < 200; i++) {
Random random=new Random();
int result=random.nextInt(10);//10是范围,但是0包含,10不包含
System.out.println(result);
}
// System.out.println(0.99*36+1);
}
}
拓展知识:
分割线
相关信息
以上就是我关于 Java-Math类 知识点的整理与总结的全部内容,另附源码
Powered by Waline v2.15.4