美文网首页
Java-API-Math类/Random类/System类

Java-API-Math类/Random类/System类

作者: 浮桥小麦 | 来源:发表于2017-06-13 14:36 被阅读37次
1.Math类
/*
        * * public static int abs(int a):绝对值
        * public static double ceil(double a)
        * public static double floor(double a)
        * public static int max(int a,int b)
        * public static double pow(double a,double b)
        * public static double random()
        * public static int round(float a)
        * public static double sqrt(double a)
        * */
        //1. PI 圆周率
        System.out.println(Math.PI);

        //2.abs绝对值
        System.out.println(Math.abs(-89));

        //3. ceil:天花板,向上进一位
        System.out.println(Math.ceil(12.8));
        System.out.println(Math.ceil(12.1));

        //4. floor:地板,向下掉一位
        System.out.println(Math.floor(12.3));
        System.out.println(Math.floor(12.9));

        //5. max:取两者中最大的
        System.out.println(Math.max(12.5,12.3));

        //6. pow: x的几次方
        System.out.println(Math.pow(3,3));

        //7. random:生成0.0到1.0的小数,左闭右开
        System.out.println(Math.random());

        //8. round:四舍五入
        System.out.println(Math.round(8.9));
        System.out.println(Math.round(8.49));
        //9. sqrt:开方
        System.out.println(Math.sqrt(9));


2. Random类
/*
        * Random:生成的是一个伪随机数,是通过算法给出的一个随机数
            *public Random()
            * public Random(long seed):种子
        * C:
            * public int nextInt()
            * public int nextInt(int n)
        *
        * */
        //1.生成一个int范围内的随机数(空参的)
        Random r = new Random();//创建一个随机数对象
        int x = r.nextInt();
        System.out.println(x);//对比方法2,其实这个不指定种子,那么Random的种子一直在不断变化。所以结果不同
        System.out.println("1--------------------------");

        //2.根据种子生成随机数(区别以一个数为基础去算随机数),同一个调用返回的是一样的
        Random r1 = new Random(100);
        int a = r1.nextInt();
        System.out.println(a);//多运行几次,算出来的a虽然是随机的,但都是一样的
        System.out.println("2--------------------------");

        //3. nextInt(int n):生成0到n-1的伪随机数
        Random r2 = new Random();
        //int b = r2.nextInt(100);
        for (int i = 0; i < 6; i++){
            int b = r2.nextInt(100);
            System.out.println(b);
        }
        System.out.println("3--------------------------");
3. Syetem类
 int[] src = {11,22,33,44,55};
        int[] dest = new int[8];
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }

        System.out.println("--------------------------");
        System.arraycopy(src, 0, dest, 0, src.length);

        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }
        demo3();
    }

    public static void demo3() {
        long start = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++) {
            System.out.println("*");
        }
        long end = System.currentTimeMillis();

        System.out.println(end - start);
    }

    public static void demo2() {
        System.exit(1);
        System.out.println("11111111111");
    }

    public static void demo1() {
        for(int i = 0; i < 100; i++) {
            new Demo();
            System.gc();
        }
    }

}

class Demo {

    @Override
    public void finalize() {
        System.out.println("清扫垃圾");
    }
}

相关文章

网友评论

      本文标题:Java-API-Math类/Random类/System类

      本文链接:https://www.haomeiwen.com/subject/saldqxtx.html