Java Math類、Random類、System類及BigDecimal類用法示例
本文實(shí)例講述了Java Math類、Random類、System類及BigDecimal類用法。分享給大家供大家參考,具體如下:
Math類
Math的方法
package cn.itcast_01;
/*
* Math:用于數(shù)學(xué)運(yùn)算的類。
* 成員變量:
* public static final double PI
* public static final double E
* 成員方法:
* public static int abs(int a):絕對(duì)值
* public static double ceil(double a):向上取整
* public static double floor(double a):向下取整
* public static int max(int a,int b):最大值 (min自學(xué))
* public static double pow(double a,double b):a的b次冪
* public static double random():隨機(jī)數(shù) [0.0,1.0)
* public static int round(float a) 四舍五入(參數(shù)為double的自學(xué))
* 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):絕對(duì)值
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));
// 需求:我要獲取三個(gè)數(shù)據(jù)中的最大值
// 方法的嵌套調(diào)用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要獲取四個(gè)數(shù)據(jù)中的最大值
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():隨機(jī)數(shù) [0.0,1.0)
System.out.println("random:" + Math.random());
// 獲取一個(gè)1-100之間的隨機(jī)數(shù)
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------");
// public static int round(float a) 四舍五入(參數(shù)為double的自學(xué))
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));
}
}
運(yùn)行結(jié)果:
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()
package cn.itcast_02;
import java.util.Scanner;
/*
* 需求:請(qǐng)?jiān)O(shè)計(jì)一個(gè)方法,可以實(shí)現(xiàn)獲取任意范圍內(nèi)的隨機(jī)數(shù)。
*
* 分析:
* A:鍵盤錄入兩個(gè)數(shù)據(jù)。
* int strat;
* int end;
* B:想辦法獲取在start到end之間的隨機(jī)數(shù)
* 我寫一個(gè)功能實(shí)現(xiàn)這個(gè)效果,得到一個(gè)隨機(jī)數(shù)。(int)
* C:輸出這個(gè)隨機(jī)數(shù)
*/
public class MathDemo {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入開始數(shù):");
int start = sc.nextInt();
System.out.println("請(qǐng)輸入結(jié)束數(shù):");
int end = sc.nextInt();
for (int x = 0; x < 100; x++) {
// 調(diào)用功能
int num = getRandom(start, end);
// 輸出結(jié)果
System.out.println(num);
}
}
/*
* 寫一個(gè)功能 兩個(gè)明確: 返回值類型:int 參數(shù)列表:int start,int end
*/
public static int getRandom(int start, int end) {
int number = (int) (Math.random() * (end - start + 1)) + start;
return number;
}
}
運(yùn)行結(jié)果:
請(qǐng)輸入開始數(shù):
100
請(qǐng)輸入結(jié)束數(shù):
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類
package cn.itcast_01;
import java.util.Random;
/*
* Random:產(chǎn)生隨機(jī)數(shù)的類
*
* 構(gòu)造方法:
* public Random():沒有給種子,用的是默認(rèn)種子,是當(dāng)前時(shí)間的毫秒值
* public Random(long seed):給出指定的種子
*
* 給定種子后,每次得到的隨機(jī)數(shù)是相同的。
*
* 成員方法:
* public int nextInt():返回的是int范圍內(nèi)的隨機(jī)數(shù)
* public int nextInt(int n):返回的是[0,n)范圍的內(nèi)隨機(jī)數(shù)
*/
public class RandomDemo {
public static void main(String[] args) {
// 創(chuàng)建對(duì)象
// 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類
系統(tǒng)類,提供了一些有用的字段和方法
運(yùn)行垃圾回收器
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("當(dāng)前的對(duì)象被回收了" + this);
super.finalize();
}
}
package cn.itcast_01;
/*
* System類包含一些有用的類字段和方法。它不能被實(shí)例化。
*
* 方法:
* public static void gc():運(yùn)行垃圾回收器。
* 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不再指定堆內(nèi)存
System.gc();
}
}
退出jvm,獲取當(dāng)前時(shí)間的毫秒值
package cn.itcast_02;
/*
* System類包含一些有用的類字段和方法。它不能被實(shí)例化。
*
* 方法:
* public static void gc():運(yùn)行垃圾回收器。
* public static void exit(int status):終止當(dāng)前正在運(yùn)行的 Java 虛擬機(jī)。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。
* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時(shí)間
* 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());
// 單獨(dú)得到這樣的實(shí)際目前對(duì)我們來說意義不大
// 那么,它到底有什么作用呢?
// 要求:請(qǐng)大家給我統(tǒng)計(jì)這段程序的運(yùn)行時(shí)間
long start = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
System.out.println("hello" + x);
}
long end = System.currentTimeMillis();
System.out.println("共耗時(shí):" + (end - start) + "毫秒");
}
}
數(shù)組復(fù)制
package cn.itcast_03;
import java.util.Arrays;
/*
* System類包含一些有用的類字段和方法。它不能被實(shí)例化。
*
* 方法:
* public static void gc():運(yùn)行垃圾回收器。
* public static void exit(int status):終止當(dāng)前正在運(yùn)行的 Java 虛擬機(jī)。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。
* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時(shí)間
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
* 從指定源數(shù)組中復(fù)制一個(gè)數(shù)組,復(fù)制從指定的位置開始,到目標(biāo)數(shù)組的指定位置結(jié)束。
*/
public class SystemDemo {
public static void main(String[] args) {
// 定義數(shù)組
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 6, 7, 8, 9, 10 };
// 請(qǐng)大家看這個(gè)代碼的意思
System.arraycopy(arr, 2, arr2, 1, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
運(yùn)行結(jié)果:
[11, 22, 33, 44, 55]
[6, 33, 44, 9, 10]
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
這篇文章主要介紹了Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式
這篇文章主要介紹了spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
mybatis使用foreach標(biāo)簽進(jìn)行嵌套插入
數(shù)據(jù)庫插入操作常見于多層架構(gòu)設(shè)計(jì)中,本文提供了一個(gè)具體的實(shí)現(xiàn)方案,涉及三層實(shí)體類結(jié)構(gòu),第一層實(shí)體類負(fù)責(zé)基本數(shù)據(jù)結(jié)構(gòu)的定義,第二層和第三層實(shí)體類則提供更詳細(xì)的業(yè)務(wù)邏輯處理,同時(shí),文章還介紹了相應(yīng)的mapper接口和配置文件設(shè)置2024-09-09
JVM優(yōu)先級(jí)線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法
這篇文章主要介紹了JVM優(yōu)先級(jí)線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
JAVA異常處理機(jī)制之throws/throw使用情況
這篇文章主要介紹了JAVA異常處理機(jī)制之throws/throw使用情況的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java實(shí)現(xiàn)通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

