20190524 Java学习

常见对象(Math 类)

  Math: 用于执行基本数学运算的方法,如初等指数,对数,平方根,三角函数。

  

System.out.println(Math.abs(-15.2));         // Math.abs()      取绝对值。
   System.out.println(Math.ceil(15.3));         // Math.ceil()     结果向上取整,是double类型。
   System.out.println(Math.floor(12.9));        // Math.ceil()     结果向下取整,是double类型。
   System.out.println(Math.max(-16.2, 16.1));   // Math.max()     比较结果大小,取最大值
   System.out.println(Math.min(-16.2, 16.1));   // Math.min()     比较结果大小,取最小值
   System.out.println(Math.pow(1,10 ));         // Math.pow()     取前面位置的次方,是double类型。
   System.out.println(Math.random());           //随机生成0.0到1.0之间的小数,包括0.0,不包括1.0。
   System.out.println(Math.round(15.8));        //四舍五入。
   System.out.println(Math.sqrt(9));            //参数的平方根。
成员方法

 常见对象(Random类)

    Random:此类用于生产随机数。

  

Random r = new Random();                 //无参的。
        int a = r.nextInt();             //随机生成正负数。
        System.out.println(a);            
               
        int x = r.nextInt(100);          // 随机生成0到n-1d的随机数,
        System.out.println(x);
        

Random d = new Random(1000);             //有参的。
        int b= d.nextInt();              //如果输出变量不变随机生成的数也不变。
        System.out.println(b);

成员方法
成员方法

 常见对象(System类)

     System:包含一些有用的类字段和方法。它不能被实例化。 

     在一个源文件中不允许定义两个用 public 修饰的类

System.exit(0);                                 //非0状态是异常终止,推出jvm

        long start = System.currentTimeMillis();        // 获取当前时间的毫秒值。
        for (int i = 0; i < 1000; i++) {
            System.out.println("*");
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);               // 计算当前方法所用的时间。

        int[] src = { 22, 55, 44, 6 };
        int[] dest = new int[8];
        System.arraycopy(src, 0, dest, 0, src.length); // 将数组内容拷贝

成员方法
成员方法

 

常见对象(BigInteger类)

   概述: 可以让超过 Integer 范围内的数据进行运行。

  构造方法: public BigInteger( String val )

  

BigInteger b1 = new BigInteger("2"); 
        BigInteger b2 = new BigInteger("1");
        System.out.println(b1.add(b2));             //  +
        System.out.println(b1.subtract(b2));        //  -
        System.out.println(b1.multiply(b2));        //  * 
        System.out.println(b1.divide(b2));          //  /(除)
        BigInteger [] arr= b1.divideAndRemainder(b2);//取除数h和余数
        for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
        
    }

成员方法
成员方法

常见对象(BigDecimal类)

    概述: 由于在运算时,floa 和 double 很容易丢失精度,所以为了精确表示,Java提供了BigDecimal。            

    构造方法: public BigDecimal( String val )

    

BigDecimal b=new BigDecimal("2");     //通过构造传入字符串方式,开发时推荐
         BigDecimal b1=new BigDecimal("1.1");
         System.out.println(b.subtract(b1));
          //通过类名 . 调用valueOf()方法
         BigDecimal a = BigDecimal.valueOf(7); // 这种方式在开发也是推荐的
         BigDecimal a1=BigDecimal.valueOf(1.1);
         System.out.println(a.subtract(a1));

成员方法
成员方法

 

猜你喜欢

转载自www.cnblogs.com/feng0001/p/10921015.html