Math和Random類常用方法

2022-04-16 23:49:25 字數 2200 閱讀 4818

1、abs 絕對值

2、acos,asin,atan,cos,sin,tan 三角函式

3、 sqrt 平方根

4、pow(double a, double b) a的b次冪

5、max(double a, double b) 取大值

6、min(double a, double b) 取小值

7、ceil(double a) 大於a的最小整數

8、floor(double a) 小於a的最大整數

9、random() 返回 0.0 到 1.0 的隨機數

10、long round(double a) double型的資料a轉換為long型(四捨五入)

11、todegrees(double angrad) 弧度->角度

12、toradians(double angdeg) 角度->弧度

//取整相關操作

system.out.

println

(math.

ceil

(3.2))

;system.out.

println

(math.

floor

(3.2))

;system.out.

println

(math.

round

(3.2))

;//3

system.out.

println

(math.

round

(3.8))

;//4

//絕對值、開方、a的b次冪等操作

system.out.

println

(math.

abs(-45

));// 絕對值(45)

system.out.

println

(math.

sqrt(64

));//平方根(8.0)

system.out.

println

(math.

pow(5,

2));

//a的b次冪(25.0)

system.out.

println

(math.

pow(2,

5));

//math類中常用的常量

system.out.

println

(math.pi)

;system.out.

println

(math.e)

;//隨機數

system.out.

println

(math.

random()

);// [0,1)0.842255731246934

random rand =

newrandom()

;//隨機生成[0,1)之間的double型別的資料

system.out.

println

(rand.

nextdouble()

);//隨機生成int型別允許範圍之內的整型資料

system.out.

println

(rand.

nextint()

);//隨機生成[0,1)之間的float型別的資料

system.out.

println

(rand.

nextfloat()

);//隨機生成false或者true

system.out.

println

(rand.

nextboolean()

);//隨機生成[0,10)之間的int型別的資料

system.out.

print

(rand.

nextint(10

));//隨機生成[20,30)之間的int型別的資料

system.out.

print(20

+ rand.

nextint(10

));//隨機生成[20,30)之間的int型別的資料(此種方法計算較為複雜)

system.out.

print(20

+(int)

(rand.

nextdouble()

*10))

;

Math和Random常用函式

math static int max int a,int b static long max long a,long b static double max double a,double b static float max float a,float b 同理min的各種過載 static t...

和日期有關的類 Math類 Random類

一 和日期有關的類 date的絕大數方法已經過時,但是沒有刪除 原因很簡單,因為過時之前可能被大量使用,刪了後果很嚴重,所以sun沒有刪除 3 dateformat 日期格式化類 作用 a 可以把日期轉換轉指定格式的字串 format b 可以把乙個字串轉換成對應的日期。parse 4 總結 dat...

常用類 Random類

random類概述及其構造方法 random類概述 此類用於產生隨機數 如果用相同的種子建立兩個 random 例項,則對每個例項進行相同的方法呼叫序列,它們將生成並返回相同的數字序列。構造方法 public random 沒有給種子,用的是預設種子,是當前時間的毫秒值 public random ...