標籤:divide 有序數組 ring main add out search 資料運算 隨機數
public class MathDemo { public static void main(String[] args) { double x=12.2; //取絕對值 System.out.println(Math.abs(-99)); //向上取整 System.out.println(Math.ceil(x)); //向下取整 System.out.println(Math.floor(x)); //取最大值 System.out.println(Math.max(2,5)); //取最小值 System.out.println(Math.min(2,5)); //取次冪 System.out.println(Math.pow(2,3)); //取隨機數[0,1) System.out.println(Math.random()); //四捨五入 System.out.println(Math.round(12.6)); System.out.println(Math.round(12.5)); System.out.println(Math.round(12.4)); //π System.out.println(Math.PI); }
import java.util.Arrays;public class ArraysDemo { public static void main(String[] args) { method2(); } public static void method1(){ int[] arr={3,6,9,11,15}; //返回有序數組中,指定值所對應的索引值。 //指定值如果不存在,索引值=(-當前索引-1) int i=Arrays.binarySearch(arr, 6); System.out.println(i); } public static void method2(){ //將數組升序排列 int[] arr={5,7,1,6,3,2,9}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); for(int a:arr){ System.out.print(a+" "); } }}
import java.math.BigInteger;
public class BigDemo { public static void main(String[]args){ BigInteger b1=new BigInteger("10000000000000000000000000000000000"); BigInteger b2=new BigInteger("20000000000000000000000000000000000"); //加法 System.out.println(b1.add(b2)); //減法 System.out.println(b1.subtract(b2)); //乘法 System.out.println(b1.multiply(b2)); //除法 System.out.println(b1.divide(b2)); }}
import java.math.BigDecimal;public class BigDecimalDemo {public static void main(String[] args) { method1(); method2(); method3(); method4();}public static void method1(){//為避免損失精度用BigDecimal計算加法BigDecimal bd1=new BigDecimal("0.09");BigDecimal bd2=new BigDecimal("0.01");System.out.println(bd1.add(bd2));}public static void method2(){//為避免損失精度用BigDecimal計算減法BigDecimal bd1=new BigDecimal("1.0");BigDecimal bd2=new BigDecimal("0.32");System.out.println(bd1.subtract(bd2));}public static void method3(){//為避免損失精度用BigDecimal計算乘法BigDecimal bd1=new BigDecimal("1.02");BigDecimal bd2=new BigDecimal("0.32");System.out.println(bd1.multiply(bd2));}public static void method4(){//為避免損失精度用BigDecimal計算除法BigDecimal bd1=new BigDecimal("1.0");BigDecimal bd2=new BigDecimal("0.3");System.out.println(bd1.divide(bd2,BigDecimal.ROUND_FLOOR,3));}}
Java基礎知識回顧-18(Math類,Arrays類和大資料運算)