Math的方法
/*
* 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));
}
}
random
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 {
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;
}
}
Random類
import java.util.Random;
/*
* Random:產(chǎn)生隨機數(shù)的類
*
* 構(gòu)造方法:
* public Random():沒有給種子,用的是默認種子,是當(dāng)前時間的毫秒值
* 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)類,提供了一些有用的字段和方法
獲取當(dāng)前時間的毫秒值
/*
* System類包含一些有用的類字段和方法。它不能被實例化。
*
* 方法:
* public static void exit(int status):終止當(dāng)前正在運行的 Java 虛擬機。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。
* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時間
*/
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) + "毫秒");
}
}