溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Java中使用Math類和Random類

發(fā)布時(shí)間:2021-05-26 09:34:18 來(lái)源:億速云 閱讀:141 作者:Leah 欄目:編程語(yǔ)言

如何在Java中使用Math類和Random類?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

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ì)我們來(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ù)制從指定的位置開始,到目標(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的特點(diǎn)有哪些

Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际?、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI