一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Java Math類、Random類、System類及BigDecimal類用法示例

Java Math類、Random類、System類及BigDecimal類用法示例

2021-07-25 15:39白楊-M Java教程

這篇文章主要介紹了Java Math類、Random類、System類及BigDecimal類用法,結合實例形式分析了java數值運算相關的Math類、Random類、System類及BigDecimal類基本功能與使用技巧,需要的朋友可以參考下

本文實例講述了java math類、random類、system類及bigdecimal類用法。分享給大家供大家參考,具體如下:

math類

math的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cn.itcast_01;
/*
 * math:用于數學運算的類。
 * 成員變量:
 * public static final double pi
 * public static final double e
 * 成員方法:
 * public static int abs(int a):絕對值
 * public static double ceil(double a):向上取整
 * public static double floor(double a):向下取整
 * public static int max(int a,int b):最大值 (min自學)
 * public static double pow(double a,double b):a的b次冪
 * public static double random():隨機數 [0.0,1.0)
 * public static int round(float a) 四舍五入(參數為double的自學)
 * public static double sqrt(double a):正平方根
 */
public class mathdemo {
 public static void main(string[] args) {
 // public static final double pi
 system.out.println("pi:" + math.pi);
 // public static final double e
 system.out.println("e:" + math.e);
 system.out.println("--------------");
 // public static int abs(int a):絕對值
 system.out.println("abs:" + math.abs(10));
 system.out.println("abs:" + math.abs(-10));
 system.out.println("--------------");
 // public static double ceil(double a):向上取整
 system.out.println("ceil:" + math.ceil(12.34));
 system.out.println("ceil:" + math.ceil(12.56));
 system.out.println("--------------");
 // public static double floor(double a):向下取整
 system.out.println("floor:" + math.floor(12.34));
 system.out.println("floor:" + math.floor(12.56));
 system.out.println("--------------");
 // public static int max(int a,int b):最大值
 system.out.println("max:" + math.max(12, 23));
 // 需求:我要獲取三個數據中的最大值
 // 方法的嵌套調用
 system.out.println("max:" + math.max(math.max(12, 23), 18));
 // 需求:我要獲取四個數據中的最大值
 system.out.println("max:"
 + math.max(math.max(12, 78), math.max(34, 56)));
 system.out.println("--------------");
 // public static double pow(double a,double b):a的b次冪
 system.out.println("pow:" + math.pow(2, 3));
 system.out.println("--------------");
 // public static double random():隨機數 [0.0,1.0)
 system.out.println("random:" + math.random());
 // 獲取一個1-100之間的隨機數
 system.out.println("random:" + ((int) (math.random() * 100) + 1));
 system.out.println("--------------");
 // public static int round(float a) 四舍五入(參數為double的自學)
 system.out.println("round:" + math.round(12.34f));
 system.out.println("round:" + math.round(12.56f));
 system.out.println("--------------");
 //public static double sqrt(double a):正平方根
 system.out.println("sqrt:"+math.sqrt(4));
 }
}

運行結果:

pi:3.141592653589793
e:2.718281828459045
--------------
abs:10
abs:10
--------------
ceil:13.0
ceil:13.0
--------------
floor:12.0
floor:12.0
--------------
max:23
max:23
max:78
--------------
pow:8.0
--------------
random:0.39060160152994794
random:75
--------------
round:12
round:13
--------------
sqrt:2.0

math.random()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.itcast_02;
import java.util.scanner;
/*
 * 需求:請設計一個方法,可以實現獲取任意范圍內的隨機數。
 *
 * 分析:
 * a:鍵盤錄入兩個數據。
 * int strat;
 * int end;
 * b:想辦法獲取在start到end之間的隨機數
 * 我寫一個功能實現這個效果,得到一個隨機數。(int)
 * c:輸出這個隨機數
 */
public class mathdemo {
 @suppresswarnings("resource")
 public static void main(string[] args) {
 scanner sc = new scanner(system.in);
 system.out.println("請輸入開始數:");
 int start = sc.nextint();
 system.out.println("請輸入結束數:");
 int end = sc.nextint();
 for (int x = 0; x < 100; x++) {
 // 調用功能
 int num = getrandom(start, end);
 // 輸出結果
 system.out.println(num);
 }
 }
 /*
 * 寫一個功能 兩個明確: 返回值類型:int 參數列表:int start,int end
 */
 public static int getrandom(int start, int end) {
 int number = (int) (math.random() * (end - start + 1)) + start;
 return number;
 }
}

運行結果:

請輸入開始數:
100
請輸入結束數:
1000
394
478
224
432
917
443
715
830
123
735
510
581
134
508
318
156
365
223
553
954
401
514
732
766
812
358
118
907
113
923
182
123
111
728
217
235
444
963
754
426
889
885
650
475
673
783
906
324
414
792
695
468
406
524
346
701
220
350
505
866
186
925
986
147
608
487
957
964
369
373
468
982
291
372
867
280
110
680
268
110
895
897
586
445
387
728
114
427
974
452
497
444
765
603
243
381
436
757
316
137

random類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package cn.itcast_01;
import java.util.random;
/*
 * random:產生隨機數的類
 *
 * 構造方法:
 * public random():沒有給種子,用的是默認種子,是當前時間的毫秒值
 * public random(long seed):給出指定的種子
 *
 * 給定種子后,每次得到的隨機數是相同的。
 *
 * 成員方法:
 * public int nextint():返回的是int范圍內的隨機數
 * public int nextint(int n):返回的是[0,n)范圍的內隨機數
 */
public class randomdemo {
 public static void main(string[] args) {
 // 創建對象
 // random r = new random();
 random r = new random(1111);
 for (int x = 0; x < 10; x++) {
 // int num = r.nextint();
 int num = r.nextint(100) + 1;
 system.out.println(num);
 }
 }
}

system類

系統類,提供了一些有用的字段和方法

運行垃圾回收器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cn.itcast_01;
public class person {
 private string name;
 private int age;
 public person() {
 super();
 }
 public person(string name, int age) {
 super();
 this.name = name;
 this.age = age;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public int getage() {
 return age;
 }
 public void setage(int age) {
 this.age = age;
 }
 @override
 public string tostring() {
 return "person [name=" + name + ", age=" + age + "]";
 }
 @override
 protected void finalize() throws throwable {
 system.out.println("當前的對象被回收了" + this);
 super.finalize();
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.itcast_01;
/*
 * system類包含一些有用的類字段和方法。它不能被實例化。
 *
 * 方法:
 * public static void gc():運行垃圾回收器。
 * public static void exit(int status)
 * public static long currenttimemillis()
 * public static void arraycopy(object src,int srcpos,object dest,int destpos,int length)
 */
public class systemdemo {
 public static void main(string[] args) {
 person p = new person("趙雅芝", 60);
 system.out.println(p);
 p = null; // 讓p不再指定堆內存
 system.gc();
 }
}

退出jvm,獲取當前時間的毫秒值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package cn.itcast_02;
/*
 * system類包含一些有用的類字段和方法。它不能被實例化。
 *
 * 方法:
 * public static void gc():運行垃圾回收器。
 * public static void exit(int status):終止當前正在運行的 java 虛擬機。參數用作狀態碼;根據慣例,非 0 的狀態碼表示異常終止。
 * public static long currenttimemillis():返回以毫秒為單位的當前時間
 * public static void arraycopy(object src,int srcpos,object dest,int destpos,int length)
 */
public class systemdemo {
 public static void main(string[] args) {
 // system.out.println("我們喜歡林青霞(東方不敗)");
 // system.exit(0);
 // system.out.println("我們也喜歡趙雅芝(白娘子)");
 // system.out.println(system.currenttimemillis());
 // 單獨得到這樣的實際目前對我們來說意義不大
 // 那么,它到底有什么作用呢?
 // 要求:請大家給我統計這段程序的運行時間
 long start = system.currenttimemillis();
 for (int x = 0; x < 100000; x++) {
 system.out.println("hello" + x);
 }
 long end = system.currenttimemillis();
 system.out.println("共耗時:" + (end - start) + "毫秒");
 }
}

數組復制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.itcast_03;
import java.util.arrays;
/*
 * system類包含一些有用的類字段和方法。它不能被實例化。
 *
 * 方法:
 * public static void gc():運行垃圾回收器。
 * public static void exit(int status):終止當前正在運行的 java 虛擬機。參數用作狀態碼;根據慣例,非 0 的狀態碼表示異常終止。
 * public static long currenttimemillis():返回以毫秒為單位的當前時間
 * public static void arraycopy(object src,int srcpos,object dest,int destpos,int length)
 * 從指定源數組中復制一個數組,復制從指定的位置開始,到目標數組的指定位置結束。
 */
public class systemdemo {
 public static void main(string[] args) {
 // 定義數組
 int[] arr = { 11, 22, 33, 44, 55 };
 int[] arr2 = { 6, 7, 8, 9, 10 };
 // 請大家看這個代碼的意思
 system.arraycopy(arr, 2, arr2, 1, 2);
 system.out.println(arrays.tostring(arr));
 system.out.println(arrays.tostring(arr2));
 }
}

運行結果:

[11, 22, 33, 44, 55]
[6, 33, 44, 9, 10]

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://www.cnblogs.com/baiyangyuanzi/p/6866850.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 污污在线免费观看 | yy3341殇情影院理论片 | 久久一er精这里有精品 | haodiaose在线精品免费视频 | 精品久久久久久久久久香蕉 | 日本成日本片人免费 | yellow最新视频2019 | 国产成人愉拍免费视频 | 日本一卡=卡三卡免费 | 成年人视频在线播放 | 三级理论在线观看 | 国产一级片视频 | 波多野结衣之双方调教在线观看 | 青青草久| 国产精品极品 | 天天做天天爱天天操 | 深夜成人 | 国产在线观看人成激情视频 | 亚洲免费在线观看视频 | 波多野结衣女教师在线观看 | 视频一区二区国产无限在线观看 | 秋霞黄色| 亚洲精品久久啪啪网站成年 | 99av导航 | 久久中文字幕综合不卡一二区 | 甜宠巨肉h文1v1校园 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 国产精品对白刺激久久久 | 99热精品国产麻豆 | 色综合久久中文字幕网 | 国产色综合久久五月色婷婷中文 | 久久天天躁狠狠躁夜夜躁 | 精品一区二区高清在线观看 | 美女扒开肌肌让男人桶 | 免费高清视频日本 | bl文全肉高h湿被灌尿 | 国产视频福利 | 色综合视频在线观看 | 日本偷偷操 | 亚洲国产成人久久综合一区 | www.91在线视频 |