一:Math类
//round 四舍五入,float时返回int值,double时返回long值
注意:操作(负数进行四舍五入时)小数位大于0.5才进位,小于等于0.5不进位
System.out.println(Math.round(-10.5)); //-10
System.out.println(Math.round(-10.51)); //-11
System.out.println(Math.round(-10.6)); //-11
System.out.println(Math.round(10.51)); //11
1 public class Demo{ 2 public static void main(String args[]){ 3 /** 4 *Math.sqrt()//计算平方根 5 *Math.cbrt()//计算立方根 6 *Math.pow(a, b)//计算a的b次方 7 *Math.max( , );//计算最大值 8 *Math.min( , );//计算最小值 9 */ 10 11 System.out.println(Math.sqrt(16)); //4.0 12 System.out.println(Math.cbrt(8)); //2.0 13 System.out.println(Math.pow(3,2)); //9.0 14 System.out.println(Math.max(2.3,4.5));//4.5 15 System.out.println(Math.min(2.3,4.5));//2.3 16 17 /** 18 * abs求绝对值 19 */ 20 System.out.println(Math.abs(-10.4)); //10.4 21 System.out.println(Math.abs(10.1)); //10.1 22 23 /** 24 * ceil返回大的值 25 */ 26 System.out.println(Math.ceil(-10.1)); //-10.0 27 System.out.println(Math.ceil(10.7)); //11.0 28 System.out.println(Math.ceil(-0.7)); //-0.0 29 System.out.println(Math.ceil(0.0)); //0.0 30 System.out.println(Math.ceil(-0.0)); //-0.0 31 System.out.println(Math.ceil(-1.7)); //-1.0 32 33 /** 34 * floor返回小的值 35 */ 36 System.out.println(Math.floor(-10.1)); //-11.0 37 System.out.println(Math.floor(10.7)); //10.0 38 System.out.println(Math.floor(-0.7)); //-1.0 39 System.out.println(Math.floor(0.0)); //0.0 40 System.out.println(Math.floor(-0.0)); //-0.0 41 42 /** 43 * random 取得一个大于或者等于0.0小于不等于1.0的随机数 44 */ 45 System.out.println(Math.random()); //小于1大于0的double类型的数 46 System.out.println(Math.random()*2);//大于0小于1的double类型的数 47 System.out.println(Math.random()*2+1);//大于1小于2的double类型的数 48 49 // rint 四舍五入,返回double值 50 //注意.5的时候会取偶数 51 System.out.println(Math.rint(10.1)); //10.0 52 System.out.println(Math.rint(10.7)); //11.0 53 System.out.println(Math.rint(10.5)); //10.0 54 System.out.println(Math.rint(10.51)); //11.0 55 System.out.println(Math.rint(-10.5)); //-10.0 56 System.out.println(Math.rint(-11.5)); //-12.0 57 System.out.println(Math.rint(-10.51)); //-11.0 58 System.out.println(Math.rint(-10.2)); //-10.0 59 60 //round 四舍五入,float时返回int值,double时返回long值 61 System.out.println(Math.round(10.1)); //10 62 System.out.println(Math.round(10.7)); //11 63 System.out.println(Math.round(10.5)); //11 64 System.out.println(Math.round(10.51)); //11 65 System.out.println(Math.round(-10.5)); //-10 66 System.out.println(Math.round(-10.51)); //-11 67 System.out.println(Math.round(-10.6)); //-11 68 } 69 }
二:Random类
示例:36选7,里面不能够有0或者重复的数据
1 package com.imooc.demo; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 6 public class RuntimeTest { 7 8 public static void main(String[] args) { 9 int index = 0 ; 10 int[] ints = new int[7]; 11 Random random = new Random(); 12 while (index < 7){ 13 int i = random.nextInt(37); 14 if (panduan(ints,i)){ 15 ints[index ++] = i; 16 } 17 } 18 Arrays.sort(ints); 19 for (int anInt : ints) { 20 System.out.println(anInt); 21 } 22 } 23 24 public static boolean panduan(int[] nums , int num){ 25 if (num == 0 )return false; 26 for (int i = 0; i < nums.length; i++) { 27 if (nums[i] == num){return false;} 28 } 29 return true; 30 } 31 32 }
三、大整数操作类:BigInteger;大浮点数:BigDecimal
假设有两个很大的数字进行计算(超过了double范围),怎么做?
先将其变为String型,而后按位去除每一个字符保存的数据进行手工的计算。(非常的难受)
Java中,专门提供了大数字操作类,BIgInteger(大整型)、BigDecimal(大浮点型)两种。
BIgInteger类的构造方法 public BigInteger(String val)//接收String型
public BigInteger[] divideAndRemainder(BigInteger val)//返回的数组,第一个为商、第二个为余数
BigDecimal类的构造方法
public BigDecimal(String val)//接收String型
public BigDecimal(double val)//接收double型
使用BigDecimal类可以使用准确的四舍五入操作。但是没直接提供的四舍五入操作方法,但是可以使用除法操作
除法操作:
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
BigDecimal divisor(被除数)
int scale(保留的小数位)
int roundingMode(进位模式)四舍五入向上进位
public static final int ROUND_HALF_UP
四:对象克隆指的是对象的复制操作,在Object类中提供。
protected Object clone() throws CloneNotSupportedException
如果要使用对象要克隆的类,没有实现较Cloneable接口,那么就会抛出CloneNotSupportedException异常。
Cloneable接口 , 标识接口,一种操作能力。
在需要被克隆的类中,重写Object类的clone方法,然后在调用即可。