前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java八大常用类(六) Math类和Random类

Java八大常用类(六) Math类和Random类

作者头像
卢衍飞
发布2023-02-14 13:59:52
1810
发布2023-02-14 13:59:52
举报
文章被收录于专栏:技术分享交流

Math类 package oop2.Mathh;

public class MathA {

代码语言:javascript
复制
public static void main(String[] args) {
    System.out.println(Math.PI);

    System.out.println(Math.E);
    System.out.println(Math.round(3.156));    //四舍五入
    System.out.println(Math.ceil(3.16));   //向上取整
    System.out.println(Math.floor(3.9));   //向下取整
    System.out.println(Math.random());   //产生随机数[0 , 1)

    System.out.println(Math.sqrt(64));  //开平方
    System.out.println(Math.pow(2,3));    //  2的3次幂
    System.out.println(Math.abs(-5));     // 去绝对值
    System.out.println(Math.max(2,9));    //比较两个数的大小
    System.out.println(Math.min(3,2));

}

} Random类 随机数 package oop2.Mathh;

import java.util.Random;

public class RandomA {

代码语言:javascript
复制
public static void main(String[] args) {
    Random random = new Random();                //   Random()括号里有数字,则为种子,之后产生的数叫伪随机数

    //产生0-10内随机整数
    int i = random.nextInt(10);                //如过是   nextInt()  ,括号没有数,将会产生int类型范围内的数
                                                        //其他类型就是nextXXXXX类型

    System.out.println(i);

    // 产生4位随机数
    int s = random.nextInt(9000)+1000;
    System.out.println(s);

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023 年 01 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档