Java SE基礎部分——常用類庫之Math和Random類隨機產生數值,mathrandom

來源:互聯網
上載者:User

Java SE基礎部分——常用類庫之Math和Random類隨機產生數值,mathrandom

 1 //20160518  Math類常用方法     練習 2 package MyPackage; 3  4 public class MathDemo {//定義主類和main方法 5  6     public static void main(String[] args) { 7         System.out.println(Math.sqrt(16));// Math類常用方法:求平方根 8         System.out.println(Math.pow(2, 3));// Math類常用方法:多少的多少次方 9         System.out.println(Math.round(13.64));// Math類常用方法:四捨五入10         System.out.println(Math.random());// Math類常用方法:產生一個隨機數11     }12 13 }

       Math類中的方法都是靜態方法,直接使用“類.方法名稱()”的形式調用即可。

---------------------------------------------------------------------------------------------

 1 //20150518   Random類:隨機產生數值     練習 2 package MyPackage; //自己定義的包 3  4 import java.util.Random; //匯入需要的Random包 5  6 public class RandomDemo { // 定義主類 7     public static void main(String[] args) { // mian方法 8         Random r = new Random(); // 執行個體化Random類的對象r 9         for (int i = 0; i < 6; i++) { // 迴圈6次輸入6個隨機數10             System.out.print(r.nextInt(100) + "\t"); // 產生隨機數並指定產生的數值範圍小於10011         }12     }13 14 }

          Random類隨機產生數值,並可以指定產生數值的範圍大小。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.