Java中常見的對象——Math

來源:互聯網
上載者:User

標籤:rand   pad   main   常見   static   bsp   top   ann   body   

Math:

用於數學運算的類。

成員變數:

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自學)

public static double pow(double a,double b):a的b次冪

public static double random():隨機數 [0.0,1.0)

public static int round(float a) 四捨五入(參數為double的自學)

public static double sqrt(double a):正平方根

  

  

demo:

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

// 需求:我要擷取三個資料中的最大值

// 方法的嵌套調用

System.out.println("max:" + Math.max(Math.max(12, 23), 18));

// 需求:我要擷取四個資料中的最大值

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():隨機數 [0.0,1.0)

System.out.println("random:" + Math.random());

// 擷取一個1-100之間的隨機數

System.out.println("random:" + ((int) (Math.random() * 100) + 1));

System.out.println("--------------");

  

// public static int round(float a) 四捨五入(參數為double的自學)

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

}

}

請設計一個方法,可以實現擷取任意範圍內的隨機數。

demo:

需求:請設計一個方法,可以實現擷取任意範圍內的隨機數。

  

分析:

A:鍵盤錄入兩個資料。

int strat;

int end;

B:想辦法擷取在start到end之間的隨機數

我寫一個功能實現這個效果,得到一個隨機數。(int)

C:輸出這個隨機數

  

public class MathDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("請輸入開始數:");

int start = sc.nextInt();

System.out.println("請輸入結束數:");

int end = sc.nextInt();

  

for (int x = 0; x < 100; x++) {

// 調用功能

int num = getRandom(start, end);

// 輸出結果

System.out.println(num);

}

}

  

/*

* 寫一個功能 兩個明確: 傳回值類型:int 參數列表:int start,int end

*/

public static int getRandom(int start, int end) {

// 回想我們講過的1-100之間的隨機數

// int number = (int) (Math.random() * 100) + 1;

// int number = (int) (Math.random() * end) + start;

// 發現有問題了,怎麼辦呢?

int number = (int) (Math.random() * (end - start + 1)) + start;

return number;

}

}

 

Java中常見的對象——Math

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.