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 @ 2022-04-25 13:12  暴躁C语言  阅读(34)  评论(0)    收藏  举报