java--->Math类,Random类

21 篇文章 1 订阅

14天阅读挑战赛

一、Math

1、使用须知:

(1)属于java.lang;下的背包,使用中无需导包!

(2)math类被final修饰,不可被继承;

(3)构造器私有化,不能创建Math类的对象:

(4)Math内部的所有的属性,方法都被static修饰:类名.直接调用,无需创建对象

2、常用属性:

                System.out.println(Math.PI);

3、常用方法:

                Math.random()——>求【0.0,1.0)的随机数;

                Math.abs(-80)——>求括号中输入的绝对值;

                Math.ceil(9.1)——>向上取值结果为“10”;

                Math.floor(9.9)——>向下取值结果为“9”;

                Math.round(3.5)——>四舍五入结果为“4”;

                Math.max(3, 6)——>取大的那个值结果为“6”;

                Math.min(3, 6)——>取小的那个值结果为“3”;

//静态导入后可以省略Math,但结果与之前一样,更加高效。

package com.msb.test03;
//静态导入:
import static java.lang.Math.*;
public class Test01 {
    public static void main(String[] args) {
        //常用方法:
        //静态导入后可以省略MATH
        System.out.println("随机数:"+random());//[0.0,1.0)
        System.out.println("绝对值"+abs(-80));
        System.out.println("向上取值"+ceil(9.1));
        System.out.println("向下取值"+floor(9.9));
        System.out.println("四舍五入"+round(3.5));
        System.out.println("取大的那个值"+max(3, 6));
        System.out.println("取小的那个值"+min(3, 6));
    }
}

4、就近原则:

        如果跟Math中方法重复了,那么会优先走本类中的方法(就近原则)

        此代码中返回值为100

public static int random(){
        return 100;
    }

5、Math内容实现代码:

package com.msb.test03;
//静态导入:
import static java.lang.Math.*;
/**
 * @Auther: Mujy
 * @Date: 2022/10/22 - 10 - 22 - 22:26
 */
public class Test01 {
    public static void main(String[] args) {
       /*//常用属性:
        System.out.println(Math.PI);

        //常用方法:
        System.out.println("随机数:"+Math.random());//[0.0,1.0)
        System.out.println("绝对值"+Math.abs(-80));
        System.out.println("向上取值"+Math.ceil(9.1));
        System.out.println("向下取值"+Math.floor(9.9));
        System.out.println("四舍五入"+Math.round(3.5));
        System.out.println("取大的那个值"+Math.max(3, 6));
        System.out.println("取小的那个值"+Math.min(3, 6));*/
        System.out.println(Math.PI);

        //常用方法:
        //静态导入后可以省略MATH
        System.out.println("随机数:"+random());//[0.0,1.0)
        System.out.println("绝对值"+abs(-80));
        System.out.println("向上取值"+ceil(9.1));
        System.out.println("向下取值"+floor(9.9));
        System.out.println("四舍五入"+round(3.5));
        System.out.println("取大的那个值"+max(3, 6));
        System.out.println("取小的那个值"+min(3, 6));
    }
    //如果跟Math中方法重复了,那么会优先走本类中的方法(就近原则)
    public static int random(){
        return 100;
    }
}

二、Random

1、利用带参数的构造器创建对象:

        代码中录入System.nanoTime(),为了保证录入值在变化,否则录入值不变,r1也不变,则输出的内容也不变

Random r1 = new Random(System.nanoTime());
        int i = r1.nextInt();
        System.out.println(i);

2、利用空参构造器创建对象:

                表面是在调用无参数构造器,实际底层还是调用了带参构造器

                (r2.nextInt(10))调用此方法可生产[0.0,输入指定值)的随即数

                //在 0(包括)和指定值(不包括)之间均匀分布的 int 值。

Random r2 = new Random();//表面是在调用无参数构造器,实际底层还是调用了带参构造器
System.out.println(r2.nextInt(10));//在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
System.out.println(r2.nextDouble());//在 0.0 和 1.0 之间均匀分布的 double 值。

3、Random内容实现代码

package com.msb.test03;

import java.util.Random;

/**
 * @Auther: Mujy
 * @Date: 2022/10/22 - 10 - 22 - 22:43
 */
public class Test02 {
    public static void main(String[] args) {
        //返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
        System.out.println("随机数:"+Math.random());

        //学习Random类
        //(1)利用带参数的构造器创建对象:
        Random r1 = new Random(System.nanoTime());
        int i = r1.nextInt();
        System.out.println(i);

        //(2)利用空参构造器创建对象:
        Random r2 = new Random();//表面是在调用无参数构造器,实际底层还是调用了带参构造器
        System.out.println(r2.nextInt(10));//在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
        System.out.println(r2.nextDouble());//在 0.0 和 1.0 之间均匀分布的 double 值。

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在摆烂的小母

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值