2022.4.22 常用类 Math类与Random类

Math

Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数。

复制代码
 1              返回值
 2 public static int abs(int a)//求绝对值
 3 public static double sqrt(double a)//开根号
 4 public static double cei1(double a)//向上取整
 5 public statit double floor (double a)//向下取整
 6 public static int max(int a,int b)//求最大值
 7                        long
 8                       float
 9                        double
10 public static int min(int a,int b)//求最小值
11 public static double pow(double a , double b)//求a的b次幂
12 public static double random()//生成随机数
13 public static int round(float a)//四舍五入
复制代码
复制代码
 1 package com.xing.Math;
 2 
 3 public class Demo01 {
 4     public static void main(String[] args) {
 5         System.out.println(Math.abs(-99));//绝对值  返回int类型
 6         System.out.println(Math.sqrt(4));//开根号  返回double类型
 7         System.out.println(Math.ceil(5.2));//向上取整   返回double类型
 8         System.out.println(Math.floor(5.2));//向下取整   返回double类型
 9         System.out.println(Math.max(4,8));//求最大值
10         System.out.println(Math.min(4,8));//求最小值
11         System.out.println(Math.pow(2,3));//求2的3次幂
12         System.out.println(Math.random());//0~1的随机数  不含1
13         System.out.println(Math.round(4.5));//四舍五入
14         
15     }
16 }
复制代码

Random

复制代码
 1 package com.xing.Math;
 2 
 3 import java.util.Random;
 4 
 5 public class Demo02 {
 6     public static void main(String[] args) {
 7         Random random = new Random();
 8         System.out.println(random.nextDouble());//与Math.random()一样
 9         System.out.println(random.nextInt());//int类型最大值与最小值之间的随机数
10         System.out.println(random.nextInt(20));//0-20之间的随机数
11         System.out.println(random.nextLong());//long类型最大值与最小值之间的随机数
12     }
13 }
复制代码

 

 

posted @   暴躁C语言  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示