009—JAVA中的Math类(知识点老细了)

Math: 数学
    java.lang
    Math类构造器私有 Math中的方法都是静态的  (可以直接使用Math.的形式进行调用)
    final class Math     (被final修饰的类不可以被继承)

public class MathTest {
    @Test
    public void test06(){
        System.out.println("Math.PI = " + Math.PI);
        System.out.println("Math.E = " + Math.E);

    }


    /*
    [22,33]
    (int)(Math.random()*(33 - 22 + 1) + 22)
    (int)(Math.random()*(n-m+1)+m);

    */


    @Test
    public void test04(){

        //开平方
        System.out.println("Math.sqrt(9) = " + Math.sqrt(9));
        System.out.println("Math.sqrt(16) = " + Math.sqrt(16));

        //x^y
        System.out.println("Math.pow(2, 3) = " + Math.pow(2, 3));
        System.out.println("Math.pow(2, 5) = " + Math.pow(2, 5));

    }


    @Test
    public void test03(){
        //俩数比较获取较大的值
        System.out.println("Math.max(10, 20) = " + Math.max(10, 20));

        System.out.println("Math.max(1, -9) = " + Math.max(1, -9));

        //俩数比较获取较小的值
        System.out.println("Math.min(1,-9) = " + Math.min(1, -9));

    }

    @Test
    public void test2(){
        //向下取整
        System.out.println("Math.floor(4.9) = " + Math.floor(4.9));
        System.out.println("Math.floor(9.6) = " + Math.floor(9.6));

        //向上取整
        System.out.println("Math.ceil(1.1) = " + Math.ceil(1.1));
        System.out.println("Math.ceil(6.2) = " + Math.ceil(6.2));
    }

    @Test
    public void test1(){
        //求绝对值
        System.out.println("Math.abs(10) = " + Math.abs(10));
        System.out.println("Math.abs(-10) = " + Math.abs(-10));
        //四舍五入 Math.round()
        System.out.println("Math.round(5.1) = " + Math.round(5.1));
        System.out.println("Math.round(5.9) = " + Math.round(5.9));
    }
}

静态导入功能

        import static java.lang.Math.*;

Random: 随机数类
        随机的布尔值
        1.创建随机对象
        2.通过对象调用不同的方法完成 随机数的效果(方法重载)
注意:
    如果种子数是相同的,那么会产生相同的随机数

(更多的方法  可以通过new random()生成对象.的方式进行查看,或者到API中进行查看)

public class RandomTest {
    //1.创建随机对象
    @Test
    public void test01(){

        //1.创建随机对象
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            //生成 int范围内的随机数
            // int num = random.nextInt();
            // int num = random.nextInt(20);//[0-19]
            // int num = random.nextInt(n);//[0,(n-1)]
            int num = random.nextInt(10);//[0-9]
            System.out.println("num = " + num);
        }
    }
    
    @Test
    public void test02() {

        //1.创建随机对象
        Random random = new Random();
        //2:生成随机数
        for (int i = 0; i < 100; i++) {

//            double v = random.nextDouble();
            boolean v = random.nextBoolean();
            System.out.println("v = " + v);
        }
    }


    @Test
    public void test04(){
        //1:生成随即对象
        Random random = new Random(1);
        //2:生成随机数
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(20);
            System.out.print(i1 + " ");
        }
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(20);
            System.out.print(i1 + " ");
        }
    }
    @Test
    public void test03(){

        //1.创建随机对象
        Random random = new Random(1);
        //2.生成随机数
        for (int i = 0; i < 10; i++) {

            int i1 = random.nextInt(20);
            System.out.print(i1+"  ");
        }
        for (int i = 0; i < 10; i++) {

            int i1 = random.nextInt(20);
            System.out.print(i1+"  ");
        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值