欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年03月22日 11:51:32   作者:白楊-M  
這篇文章主要介紹了Java Math類、Random類、System類及BigDecimal類用法,結(jié)合實例形式分析了java數(shù)值運算相關(guān)的Math類、Random類、System類及BigDecimal類基本功能與使用技巧,需要的朋友可以參考下

本文實例講述了Java Math類、Random類、System類及BigDecimal類用法。分享給大家供大家參考,具體如下:

Math類

Math的方法

package cn.itcast_01;
/*
 * Math:用于數(shù)學(xué)運算的類。
 * 成員變量:
 * 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自學(xué))
 * public static double pow(double a,double b):a的b次冪
 * public static double random():隨機數(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):絕對值
 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));
 // 需求:我要獲取三個數(shù)據(jù)中的最大值
 // 方法的嵌套調(diào)用
 System.out.println("max:" + Math.max(Math.max(12, 23), 18));
 // 需求:我要獲取四個數(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():隨機數(shù) [0.0,1.0)
 System.out.println("random:" + Math.random());
 // 獲取一個1-100之間的隨機數(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));
 }
}

運行結(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;
/*
 * 需求:請設(shè)計一個方法,可以實現(xiàn)獲取任意范圍內(nèi)的隨機數(shù)。
 *
 * 分析:
 * A:鍵盤錄入兩個數(shù)據(jù)。
 * int strat;
 * int end;
 * B:想辦法獲取在start到end之間的隨機數(shù)
 * 我寫一個功能實現(xiàn)這個效果,得到一個隨機數(shù)。(int)
 * C:輸出這個隨機數(shù)
 */
public class MathDemo {
 @SuppressWarnings("resource")
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.println("請輸入開始數(shù):");
 int start = sc.nextInt();
 System.out.println("請輸入結(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);
 }
 }
 /*
 * 寫一個功能 兩個明確: 返回值類型: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;
 }
}

運行結(jié)果:

請輸入開始數(shù):
100
請輸入結(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)生隨機數(shù)的類
 *
 * 構(gòu)造方法:
 * public Random():沒有給種子,用的是默認種子,是當前時間的毫秒值
 * public Random(long seed):給出指定的種子
 *
 * 給定種子后,每次得到的隨機數(shù)是相同的。
 *
 * 成員方法:
 * public int nextInt():返回的是int范圍內(nèi)的隨機數(shù)
 * public int nextInt(int n):返回的是[0,n)范圍的內(nèi)隨機數(shù)
 */
public class RandomDemo {
 public static void main(String[] args) {
 // 創(chuàng)建對象
 // 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)類,提供了一些有用的字段和方法

運行垃圾回收器

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();
 }
}

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不再指定堆內(nèi)存
 System.gc();
 }
}

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

package cn.itcast_02;
/*
 * System類包含一些有用的類字段和方法。它不能被實例化。
 *
 * 方法:
 * public static void gc():運行垃圾回收器。
 * public static void exit(int status):終止當前正在運行的 Java 虛擬機。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。
 * 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());
 // 單獨得到這樣的實際目前對我們來說意義不大
 // 那么,它到底有什么作用呢?
 // 要求:請大家給我統(tǒng)計這段程序的運行時間
 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) + "毫秒");
 }
}

數(shù)組復(fù)制

package cn.itcast_03;
import java.util.Arrays;
/*
 * System類包含一些有用的類字段和方法。它不能被實例化。
 *
 * 方法:
 * public static void gc():運行垃圾回收器。
 * public static void exit(int status):終止當前正在運行的 Java 虛擬機。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。
 * public static long currentTimeMillis():返回以毫秒為單位的當前時間
 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 * 從指定源數(shù)組中復(fù)制一個數(shù)組,復(fù)制從指定的位置開始,到目標數(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 };
 // 請大家看這個代碼的意思
 System.arraycopy(arr, 2, arr2, 1, 2);
 System.out.println(Arrays.toString(arr));
 System.out.println(Arrays.toString(arr2));
 }
}

運行結(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é)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • eclipse中沒有SERVER的解決辦法(超詳細)

    eclipse中沒有SERVER的解決辦法(超詳細)

    使用eclipse進行tomcat配置時,經(jīng)常會發(fā)現(xiàn)一個重要的問題就是打開eclipse之后沒有了server選項,所以本給大家詳細介紹了eclipse中沒有SERVER的解決辦法,文中有詳細的圖文講解,需要的朋友可以參考下
    2023-12-12
  • SpringBoot FreeWorker模板技術(shù)解析

    SpringBoot FreeWorker模板技術(shù)解析

    這篇文章主要介紹了SpringBoot FreeWorker模板技術(shù)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 淺談Java的WeakHashMap源碼

    淺談Java的WeakHashMap源碼

    這篇文章主要介紹了淺談Java的WeakHashMap源碼,WeakHashMap,從名字可以看出它是某種?Map,它的特殊之處在于?WeakHashMap?里的entry可能會被GC自動刪除,即使程序員沒有調(diào)用remove()或者clear()方法,需要的朋友可以參考下
    2023-09-09
  • SpringBoot基于Redis的分布式鎖實現(xiàn)過程記錄

    SpringBoot基于Redis的分布式鎖實現(xiàn)過程記錄

    Redis是一套 key-value 高性能數(shù)據(jù)庫,使用它可以大大提高我們的開發(fā)效率,在SpringBoot中,自動配置也幫我們節(jié)約了大量的配置,下面這篇文章主要給大家介紹了關(guān)于SpringBoot基于Redis的分布式鎖實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java單例模式的知識點詳解

    Java單例模式的知識點詳解

    在本篇文章里小編給大家整理的是關(guān)于Java單例模式的知識點詳解,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • 一文精通Java 多線程之全方位解讀

    一文精通Java 多線程之全方位解讀

    Java 給多線程編程提供了內(nèi)置的支持。 一條線程指的是進程中一個單一順序的控制流,一個進程中可以并發(fā)多個線程,每條線程并行執(zhí)行不同的任務(wù),多線程是多任務(wù)的一種特別的形式,但多線程使用了更小的資源開銷
    2021-10-10
  • SpringBoot中@Conditional注解的使用

    SpringBoot中@Conditional注解的使用

    這篇文章主要介紹了SpringBoot中@Conditional注解的使用,@Conditional注解是一個條件裝配注解,主要用于限制@Bean注解在什么時候才生效,以指定的條件形式控制bean的創(chuàng)建,需要的朋友可以參考下
    2024-01-01
  • Java 線程相關(guān)總結(jié)

    Java 線程相關(guān)總結(jié)

    這篇文章主要介紹了Java 線程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Java框架設(shè)計靈魂之反射的示例詳解

    Java框架設(shè)計靈魂之反射的示例詳解

    反射就是把Java類中的各個成員映射成一個個的Java對象。本文將通過示例為大家詳細講解Java框架設(shè)計的靈魂:反射,感興趣的可以了解一下
    2022-06-06
  • spring集成okhttp3的步驟詳解

    spring集成okhttp3的步驟詳解

    okhttp是一個封裝URL,比HttpClient更友好易用的工具,下面這篇文章主要給大家介紹了關(guān)于spring集成okhttp3的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2018-04-04

最新評論