1. 程式人生 > 其它 >Java學習筆記_流程控制_順序結構、選擇結構

Java學習筆記_流程控制_順序結構、選擇結構

java Math類的常用方法介紹
程式碼塊

public class MainTest {
public static void main(String[] args) {
//求sin值
double sin = Math.sin(3.14);
System.out.println("sin3.14=" + sin);
//求cos值
double cos = Math.cos(0);
System.out.println("cos0=" + cos);
//求tan值
double tan = Math.tan(0.785);
System.out.println("tan0.785=" + tan);
//求arcsin
double arcsin = Math.asin(1);
System.out.println("arcsin1 = " + arcsin);
//求arccos
double arccos = Math.acos(1);
System.out.println("arccos = " + arccos);
//求arctan
double arctan = Math.atan(30);
System.out.println("arctan30 = " + arctan);
//求弧度
double radians = Math.toRadians(180);
System.out.println("180度角的弧度為" + radians);
//求角度
double angle = Math.toDegrees(3.141592653589793);
System.out.println("π的角度數為" + angle);
//求以e為底的指數
double exp = Math.exp(1);
System.out.println("以e為底指數為1的數為" + exp);
//求以e為底e的平方的對數
double log = Math.log(Math.E * Math.E);
System.out.println("以e為底e的平方的對數" + log);
//求以10為底100的對數
double log10 = Math.log10(100);
System.out.println("以10為底100的對數" + log10);
//求100的平方根
double sqrt = Math.sqrt(100);
System.out.println("100的平方根是" + sqrt);
//求27的立方根
double cbrt = Math.cbrt(27);
System.out.println("27的立方根是" + cbrt);
//求10除以3的餘數
double rest = Math.IEEEremainder(10, 3);
System.out.println("10除以3的餘數為" + rest);
//求0.9向上取整
double ceil = Math.ceil(0.9);
System.out.println("0.9向上取整" + ceil);
//求2.49向下取整
double floor = Math.floor(2.49);
System.out.println("2.49向下取整" + floor);
//求最接近引數的整數值(若有兩個滿足條件的資料則取為偶數的資料)
double rint = Math.rint(3.5);
System.out.println("最接近引數的整數值" + rint);
//獲得(1,1)座標與x軸夾角度數
double atan2 = Math.atan2(1, 1);
System.out.println("座標(1,1)的極座標為" + atan2);
//求3的5次方
double pow = Math.pow(3, 5);
System.out.println("3的5次方" + pow);
//4舍5入
double round = Math.round(3.5);
System.out.println("3.5四捨五入為" + round);
//計算2<<5+3<<4(會丟擲結果溢位異常)------- 2<<5:2乘以2的5次方 3<<4:3乘以2的4次方
int sum = Math.addExact(2 << 5, 3 << 4);
System.out.println("2<<5+3<<4=" + sum);
//計算2<<5-3<<4(會丟擲結果溢位異常)
int subTract = Math.subtractExact(2 << 5, 3 << 4);
System.out.println("2<<5-3<<4=" + subTract);
//計算2<<5*3<<4
int multiply = Math.multiplyExact(2 << 5, 3 << 4);
System.out.println("2<<5*3<<4=" + multiply);
//計算2<<5加1(會跑出溢位異常)
int increment = Math.incrementExact(2 << 5);
System.out.println("2<<5+1 = " + increment);
//計算2<<5加1(會跑出溢位異常)
int decrementExact = Math.decrementExact(2 << 5);
System.out.println("2<<5-1 = " + decrementExact);
//計算2<<5的相反數
int negate = Math.negateExact(2 << 5);
System.out.println("2<<5的相反數是" + negate);
//long轉int 2<<15
int intNum = Math.toIntExact(2 << 15L);
System.out.println(intNum);

//2&3 2在2進制中為10 3為11 & 表示相同位都為1則為1,其他為0 10&11 2進位制第二位都為1 所以 2&3=10=2

//multiplyHigh意義未知
//計算2<<5除以2<<4(取整)
int floorDiv = Math.floorDiv(2<<5,2<<4);
System.out.println("2<<5/2<<4="+floorDiv);
//取模運算(演算法與取餘相同,但負數運算是計算方式不同)
int floorMod = Math.floorMod(10,3);
System.out.println("10/3的模="+floorMod);
//取-52的絕對值
int abs = Math.abs(-52);
System.out.println("-52的絕對值="+abs);
//比較2<<5和3<<4的大小
int max = Math.max(2<<5,3<<4);
System.out.println("2<<5與3<<4相比,較大的數為"+max);
//比較2<<5和3<<4的大小
int min = Math.min(2<<5,3<<4);
System.out.println("2<<5與3<<4相比,較小的數為"+min);
//計算3乘5加4
double fma = Math.fma(3,5,4);
System.out.println("3乘5加4="+fma);
}
}

執行結果

sin3.14=0.0015926529164868282
cos0=1.0
tan0.785=0.9992039901050427
arcsin1 = 1.5707963267948966
arccos = 0.0
arctan30 = 1.5374753309166493
180度角的弧度為3.141592653589793
π的角度數為180.0
以e為底指數為1的數為2.718281828459045
以e為底e的平方的對數2.0
以10為底100的對數2.0
100的平方根是10.0
27的立方根是3.0
10除以3的餘數為1.0
0.9向上取整1.0
2.49向下取整2.0
最接近引數的整數值4.0
座標(1,1)的極座標為0.7853981633974483
3的5次方243.0
3.5四捨五入為4.0
2<<5+3<<4=112
2<<5-3<<4=16
2<<5*3<<4=3072
2<<5+1 = 65
2<<5-1 = 63
2<<5的相反數是-64
65536
2<<5/2<<4=2
10/3的模=1
-52的絕對值=52
2<<5與3<<4相比,較大的數為64
2<<5與3<<4相比,較小的數為48
3乘5加4=19.0

所有 Number 派生類 parseInt 方法格式類似如下:

static int parseInt(String s)

static int parseInt(String s, int radix)

引數

  • s-- 十進位制表示的字串。

  • radix-- 指定的基數。

返回值

  • parseInt(String s):返回用十進位制引數表示的整數值。

  • parseInt(int i):使用指定基數的字串引數表示的整數 (基數可以是 10, 2, 8, 或 16 等進位制數) 。

例項

public class Test{
public static void main(String args[]){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);

System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}

編譯以上程式,輸出結果為:

9
5.0
1092