最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年03月22日 11:51:32   作者:白楊-M  
這篇文章主要介紹了Java Math類、Random類、System類及BigDecimal類用法,結(jié)合實(shí)例形式分析了java數(shù)值運(yùn)算相關(guān)的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:鍵盤(pán)錄入兩個(gè)數(shù)據(jù)。
 * int strat;
 * int end;
 * B:想辦法獲取在start到end之間的隨機(jī)數(shù)
 * 我寫(xiě)一個(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)輸入開(kāi)始數(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);
 }
 }
 /*
 * 寫(xiě)一個(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)輸入開(kāi)始數(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():沒(méi)有給種子,用的是默認(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ì)我們來(lái)說(shuō)意義不大
 // 那么,它到底有什么作用呢?
 // 要求:請(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ù)制從指定的位置開(kāi)始,到目標(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)文章

  • 解決對(duì)接JAVA SM2加密遇到的坑

    解決對(duì)接JAVA SM2加密遇到的坑

    這篇文章主要介紹了解決對(duì)接JAVA SM2加密遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法

    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格式的配置方式

    這篇文章主要介紹了spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • java 字符串分割的三種方法(總結(jié))

    java 字符串分割的三種方法(總結(jié))

    下面小編就為大家?guī)?lái)一篇java 字符串分割的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • mybatis使用foreach標(biāo)簽進(jìn)行嵌套插入

    mybatis使用foreach標(biāo)簽進(jìn)行嵌套插入

    數(shù)據(jù)庫(kù)插入操作常見(jiàn)于多層架構(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)方法

    這篇文章主要介紹了JVM優(yōu)先級(jí)線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • RabbitMQ的安裝和配置可視化界面的詳細(xì)步驟

    RabbitMQ的安裝和配置可視化界面的詳細(xì)步驟

    這篇文章主要介紹了RabbitMQ的安裝和配置可視化界面的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • JAVA異常處理機(jī)制之throws/throw使用情況

    JAVA異常處理機(jī)制之throws/throw使用情況

    這篇文章主要介紹了JAVA異常處理機(jī)制之throws/throw使用情況的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java實(shí)現(xiàn)通訊錄管理系統(tǒng)

    java實(shí)現(xiàn)通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Spring Boot中如何使用Swagger詳解

    Spring Boot中如何使用Swagger詳解

    Swagger是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful風(fēng)格的Web服務(wù),這篇文章主要給大家介紹了關(guān)于Spring Boot中如何使用Swagger的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評(píng)論

义乌市| 定襄县| 隆安县| 普洱| 武夷山市| 平定县| 静乐县| 自治县| 金阳县| 清新县| 余干县| 长子县| 三明市| 克什克腾旗| 城口县| 新干县| 全州县| 朝阳县| 罗城| 南溪县| 兖州市| 焉耆| 晴隆县| 青神县| 绥中县| 郑州市| 夏河县| 扎鲁特旗| 英吉沙县| 曲阜市| 林州市| 周口市| 习水县| 芜湖县| 察隅县| 杭锦后旗| 巴林右旗| 闻喜县| 肇州县| 焦作市| 延吉市|