Java學習總結:35(數字操作類)

Java的數字操作類

一.Math類

Math類是一個專門用來進行數學計算的操作類,它提供了一系列的數學計算方法。在Math類裏面提供的一切方法都是static型方法,所以可以直接由類名稱進行調用。

例:觀察四捨五入操作

package Project.Study.MathClass;

public class Test1 {
    public static void main(String[]args){
        System.out.println(Math.round(15.5));
        System.out.println(Math.round(-15.51));
        System.out.println(Math.round(-15.4));
        System.out.println(Math.round(-15.45));
        System.out.println(Math.round(-15.6));
        System.out.println(Math.round(15.46));
        System.out.println(Math.round(-15.5));
    }
}
//結果:
//16
//-16
//-15
//-15
//-16
//15
//-15

由上程序我們可以發現,只有當操作數據小數位大於0.5才進位,小於或等於0.5則不進位。

例:實現指定位數的四捨五入操作

package Project.Study.MathClass;

public class Test2 {
    public static void main(String[]args){
        System.out.println(round(-15.678139,2));
    }
    /**
     * 四捨五入操作,可以保留指定長度的小數位數
     * @param num 要進行四捨五入操作的數字
     * @param scale 保留的小數位
     * @return 四捨五入後的數據
     */
    public static double round(double num,int scale){
        //Math.pow()的方法作用是進行10的N次方的計算
        return Math.round(num*Math.pow(10.0,scale))/Math.pow(10.0,scale);
    }
}
//結果:
//-15.68

二.Random類

Random類是一個專門負責產生隨機數的操作類

Random類的常用方法:

No. 方法 類型 描述
1 public Random() 構造 創建一個新的Random實例
2 public Random(long seed) 構造 創建一個新的Random實例並且設置一個種子數
3 public int nextInt(int bound) 普通 產生一個不大於指定邊界的隨機整數

例:產生10個不大於100的正整數

package Project.Study.RandomClass;

import java.util.Random;

public class Test1 {
    public static void main(String[]args){
        Random rand=new Random();
        for(int x=0;x<10;x++){
            System.out.print(rand.nextInt(100)+"、");
        }
    }
}
//結果:
//89、93、9、96、33、76、10、80、41、87、

例:選號系統(36選7)

package Project.Study.RandomClass;

import java.util.Random;

public class Test2 {
    public static void main(String[]args){
        Random rand=new Random();
        int[] data =new int[7]; //開闢一個7個元素的數組,保存生成數字
        int foot=0;             //數組操作腳標
        while(foot<7){
            int t=rand.nextInt(37);//生成一個不大於37的隨機數
            if(!isRepeat(data,t)){  //重複的話就不執行
                data[foot++]=t;     //保存數據
            }
        }
        java.util.Arrays.sort(data);//排序
        for (int datum : data) {
            System.out.print(datum + "、");
        }
    }
    /**
     * 此方法主要是判斷是否存在重複內容
     * @param temp 指的是已經保存的數據
     * @param num 新生成的數據
     * @return 如果存在返回true,否則返回false
     */
    public static boolean isRepeat(int[] temp, int num){
        if(num==0){         //沒有必要判斷了
            return true;    //直接返回,隨後的代碼都不再執行
        }
        for (int i : temp) {
            if (i == num) {
                return true;//表示後面的數據都不再進行判斷
            }
        }
        return false;
    }
}
//結果:
//2、9、12、13、18、28、31、

三.大數字操作類

1.大整數操作類:BigInteger

大整數可以操作無窮大的整型數據。

BigInteger類的基本操作方法:

No. 方法 類型 描述
1 public BigInteger(String val) 構造 實例化BigInteger對象
2 public BigInteger add(BigInteger val) 普通 加法操作
3 public BigInteger subtract(BigInteger val) 普通 減法操作
4 public BigInteger multiply(BigInteger val) 普通 乘法操作
5 public BigInteger divide(BigInteger val) 普通 除法操作(不保留餘數)
6 public BigInteger [] divideAndRemainder(BigInteger val) 普通 除法操作(保留餘數),數組第一個元素是商,第二個元素是餘數

例:進行大整數計算

package Project.Study.BigIntegerClass;

import java.math.BigInteger;

public class Test1 {
    public static void main(String[]args){
        BigInteger bigA=new BigInteger("123456789013213214214");	//大數字A
        BigInteger bigB=new BigInteger("2324342314132432432");		//大數字B
        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]);
    }
}
//結果:
//加法操作:125781131327345646646
//減法操作:121132446699080781782
//乘法操作:286955838670331461717477157288896988448
//除法操作:53
//商:53,餘數:266646364194295318

2.大小數操作類:BigDecimal

使用BigDecimal類最常進行的操作便是進行準確位數的四捨五入計算。

使用BigDecimal完成四捨五入操作:

No. 方法及常量 類型 描述
1 public static final int ROUND_HALF_UP 常量 向上進位
2 public BigDecimal(double val) 構造 傳遞一個double型數據
3 public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) 普通 除法操作,參考意義如下:BigDecimal divisor:被除數; int scale:保留的小數位長度;int roundingMode: 進位模式

例:完成準確的四捨五入操作

ckage Project.Study.BigDecimalClass;

import java.math.BigDecimal;

class MyMath{
    public static double round(double num,int scale){
        BigDecimal big=new BigDecimal(num);				//將數據封裝在BigDecimal類中
        BigDecimal result= big.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);								//除法計算
        return result.doubleValue();					//Number類的方法
    }
}
public class Test1 {
    public static void main(String[]args){
        System.out.println(MyMath.round(15.5,0));
        System.out.println(MyMath.round(-15.5,0));
        System.out.println(MyMath.round(1168.4344343,4));
    }
}
//結果:
//16.0
//-16.0
//1168.4344
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章