数学操作类(Math、Random、大数字操作类)
1. Math 类(了解)
专门进行数学计算的操作类。
所有方法都是 static 方法。因为 Math 类没有普通属性。
比较要注意的是四舍五入:
public static long round(double a) { }
对于正数而言,大于等于 0.5 就进 1
对于负数而言,小数位大于 0.5 才进位(不包含等于!只有 java 的 Math 会这样)
public class Main{
public static void main(String[] args){
System.out.println(Math.round(15.5));
System.out.println(Math.round(-15.5));
System.out.println(Math.round(-15.51));
}
}
out:
16
-15
-16
Process finished with exit code 0
2. Random 类
主要功能是取得随机数的操作
例子:产生 10 个不大于 100 的正整数
public class Main{
public static void main(String[] args){
Random rand = new Random();
for (int i = 0 ; i< 10 ; i++){
System.out.print(rand.nextInt(100) + "、" );
}
}
}
out:
44、59、79、94、54、67、71、56、9、86、
Process finished with exit code 0
例子:36 选 7
最大值是 36,边界值是 37,并且里面不能有 0 或者是重复的数据。
import java.util.Random;
public class Main{
public static void main(String[] args){
Random rand = new Random();
int data[] = new int[7];
int foot = 0;
while(foot < 7){
int t = rand.nextInt(37);
if (!isRepeat(data,t)){
data[ foot ++ ] = t;
}
}
java.util.Arrays.sort(data);
for (int i = 0 ; i< data.length;i++){
System.out.println(data[i]+"、");
}
}
/**
* 判断是否存在重复的内容,但是不允许保存0
* @param temp 指的是已经保存的数据
* @param num 新生成的数据
* @return 存在true,否则返回false
*/
public static boolean isRepeat(int temp[],int num){
if (num==0){
return true;
}
for (int i = 0; i < temp.length ; i++){
if (temp[i] == num){
return true;
}
}
return false;
}
}
3. BigInteger 大整数操作类
如果数值很大,首先考虑使用 double。但是超出之后,用什么?
class Main {
public static void main(String [] args){
System.out.println(Double.MAX_VALUE*Double.MAX_VALUE);
}
}
out:
Infinity
Process finished with exit code 0
首先不使用 Double 保存,只有使用 String 进行保存。
java.math 中提供了 BigInteger 和 BigDecimal 类进行大数操作(可以接受 String 构造)
例子:
import java.math.BigInteger;
class Main {
public static void main(String [] args){
BigInteger bigA = new BigInteger("2212316548977456");
BigInteger bigB = new BigInteger("98756456");
System.out.println(bigA.add(bigB));
System.out.println(bigA.subtract(bigB));
System.out.println(bigA.multiply(bigB));
System.out.println(bigA.divide(bigB));
//返回两个数,一个商,一个余数
BigInteger result[] = bigA.divideAndRemainder(bigB);
System.out.println("商:"+ result[0] + " , 余数:"+result[1]);
}
}
out:
2212316647733912
2212316450221000
218480541927163978455936
22401740
商:22401740 , 余数:98344016
Process finished with exit code 0
4. BigDecimal 大浮点数
BigInteger 不能存小数,BigDecimal 可以保存小数。
- 构造 1 接受 String
- 构造 2 接受 double
但是 BigInteger 不能接受 int 构造
BigDecimal 也支持大量基础数学计算。
但是利用 BigDecimal 进行准确的四舍五入操作 !
但是没有直接的提供四舍五入的操作,我们可以利用除法操作来实现。
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
return divide(divisor, scale, roundingMode.oldMode);
}
BigDecimal divisor:是被除数
int scale:保留的小数位数
RoundingMode roundingMode:进位模式
进行准确的四舍五入
/**
* 实现准确的四舍五入操作
* @param num 操作数
* @param scale 保留的小数位数
* @return
*/
public static double round(double num ,int scale){
BigDecimal bigA = new BigDecimal(num);
BigDecimal bigB = new BigDecimal(1);
return bigA.divide(bigB, scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}