java常用类-- Math和Random

Math类常用方法

在这里插入图片描述

package cn.usts.edu.fly.MathAndRondom;

/**
 * @author :fly
 * @description: Math类常用方法
 * @date :2021/10/31 15:52
 */
public class MathDemo {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.abs(-12));
        System.out.println(Math.sin(45));//sin cos tan ....
        System.out.println(Math.pow(2,3));// 2^3
        System.out.println(Math.ceil(2.6));//向下取整 最小
        System.out.println(Math.floor(3.2));//向上取整 最大
        System.out.println(Math.round(3.5));//四舍五入
        System.out.println(Math.max(1.1,22.2));//最大值
        System.out.println(Math.min(1.1,22.2));//最小值
        System.out.println(Math.random());// 随机0-1
    }
}

Random类的常用方法

在这里插入图片描述

package cn.usts.edu.fly.MathAndRondom;

import java.util.Random;

/**
 * @author :fly
 * @description: Random类的常用方法
 * @date :2021/10/31 16:04
 */
public class RandomDemo {
    
    
    public static void main(String[] args) {
    
    
        Random random = new Random();
        System.out.println("随机的布尔值:"+random.nextBoolean());
        System.out.println("随机的布尔值:"+random.nextDouble());
        System.out.println("随机的Float:"+random.nextFloat());
        System.out.println("随机的Int():"+random.nextInt());
        System.out.println("随机的0-10 int:"+random.nextInt(10));
        System.out.println("随机的Long值:"+random.nextLong());
        


    }
}

猜你喜欢

转载自blog.csdn.net/qq_43619461/article/details/121065475