Java Math類,Random類,System類

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) + "毫秒");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,775評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評論 19 139
  • 方法1 (數(shù)據(jù)類型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math...
    GB_speak閱讀 41,406評論 2 6
  • 14.01_常見對象(正則表達式的概述和簡單使用) A:正則表達式是指一個用來描述或者匹配一系列符合某個語法規(guī)則的...
    苦笑男神閱讀 705評論 0 0
  • 有一種歌者,稱其為singer似乎有些味寡,只有troubadour這種古老的概念才能勉強詮釋。他們,用音符產(chǎn)生思...
    allenlynd閱讀 339評論 0 0

友情鏈接更多精彩內(nèi)容