网站首页 > 知识剖析 正文
/**
* 测试Math类的常用方法
* 测试Random类
*/
public class TestMath {
public static void main(String[] args) {
int a = 4;
int b = -5;
int c = 2;
double d = 1.1;
double e = 1.5;
System.out.println(Math.abs(b)+"Math.abs()取绝对值 结果为5 返回结果为同数据类型");
System.out.println(Math.sqrt(a)+"Math.sqrt()取平方根 4开平方结果为2.0 返回double");
System.out.println(Math.pow(c,a)+"Math.pow()幂运算 返回c的a次方 结果为16.0 返回double");
System.out.println(Math.max(a,c)+"Math.max()取两数最大值 结果为4 返回同数据类型");
System.out.println(Math.min(a,c)+"Math.min()取两数最小值 结果为2 返回同数据类型");
System.out.println(Math.ceil(d)+"Math.ceil()取大于d的最小整数 结果为2.0 返回double");
System.out.println(Math.floor(e)+"Math.floor()取小于e的最大整数 结果为1.0 返回double");
System.out.println(Math.random()+"取[0,1)的随机数 返回double 范围包含0不包含1");
System.out.println(Math.round(e)+"Math.round()四舍五入 结果为2 返回long");
System.out.println(Math.toDegrees(Math.PI)+"Math.toDegrees()将弧度转为角度 PI对应3.14 结果为180.0 返回double");
System.out.println(Math.toRadians(45)+"Math.toRadians()将角度转为弧度 45度等于PI/4 结果为0.7853981633974483 返回double");
System.out.println(Math.sin(Math.toRadians(30))+"Math.sin()求弧度的sin值 对边除以斜边 用Math.toRadians将30度转换为弧度 结果为0.49999999999999994 返回double");
System.out.println(Math.cos(Math.PI/4)+"Math.cos()求弧度的cos值 邻边除以斜边 PI/4是45度 结果为0.7071067811865476 返回double");
System.out.println(Math.tan(Math.PI/4)+"Math.tan()求弧度的tan值 对边除以邻边 结果为0.9999999999999999 返回double");
System.out.println(Math.toDegrees(Math.asin(0.5))+"Math.asin()求arcsin值 通过已知sin值求弧度 返回double弧度 加toDegrees()转为角度 结果为30.000000000000004");
System.out.println(Math.toDegrees(Math.acos(1/Math.sqrt(2)))+"Math.acos()求arccos值 返回double弧度 1除以根号2是45度 结果为45.00000000000001");
System.out.println(Math.atan(3.0/4)+"Math.atan()求arctan值 返回double弧度 结果为0.6435011087932844");
Random ran = new Random();
//Random类 用于产生随机数
System.out.println(ran.nextDouble()+"随机生成[0,1)的小数 作用同Math.random");
System.out.println(ran.nextInt()+"随机生成int值 正负21亿之间随机取");
System.out.println(ran.nextBoolean()+"随机生成true或者false");
System.out.println(ran.nextInt(10)+"随机生成[0,10)区间的整数 不包含10 随机0-9");
System.out.println(20+ran.nextInt(6)+"随机生成20-25的整数 .nextInt(6)是0-5 +20变成20-25");
}
}
猜你喜欢
- 2025-06-10 LeetCode 力扣官方题解 | 521. 最长特殊序列Ⅰ
- 2025-06-10 进一步理解函数(进一步理解函数的方法)
- 2025-06-10 二维码能跑游戏?开发者 3KB 复刻 DOOM 崩溃,ChatGPT 救场
- 2025-06-10 数据结构:布隆过滤器(Bloom Filter)
- 2025-06-10 数据质量动态探查及相关前端实现(数据质量检查方法)
- 2025-06-10 HarmonyOS实战:实现任意拖动的应用悬浮窗口
- 2025-06-10 一文搞懂堆外内存(模拟内存泄漏)(堆内存 堆外内存)
- 2025-06-10 JavaScript中计算数组中的最大值/最小值
- 2025-06-10 JavaScript展开运算符与剩余参数,灵活操作数组与对象的终极利器
- 2025-06-10 为什么 MapReduce 再次流行起来了?
- 08-0612 个最佳 JavaScript 动画库,让您的 Web 页面动起来
- 08-06HTML 二次函数图像动画展示
- 08-06UnoCSS 内置的动画
- 08-06炫酷的CSS3 loading加载动画,总有一款适合你
- 08-06想要开发更好的Python项目,代码质量是关键
- 08-06想要入门学好Python编程,先从这几本书开始
- 08-06甲方VS程序员精彩画面鉴赏
- 08-06后端语言性能排行,哪种语言最快,为什么?
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)