Java Math類,Random類,System類

Math的方法

/*

  • Math:用于數(shù)學(xué)運(yùn)算的類。
  • 成員變量:
  •     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():隨機(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):絕對值
    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));
}

}
random

import java.util.Scanner;

/*

  • 需求:請?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 {
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);
    }
}

/*
 * 寫一個(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;
}

}
Random類

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)建對象
// 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)前時(shí)間的毫秒值

/*

  • System類包含一些有用的類字段和方法。它不能被實(shí)例化。
  • 方法:
  •    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 class SystemDemo {
public static void main(String[] args) {
// System.out.println("我們喜歡林青霞(東方不敗)");
// System.exit(0);
// System.out.println("我們也喜歡趙雅芝(白娘子)");

    // System.out.println(System.currentTimeMillis());

    // 單獨(dú)得到這樣的實(shí)際目前對我們來說意義不大
    // 那么,它到底有什么作用呢?
    // 要求:請大家給我統(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) + "毫秒");
}

}

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

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

  • 方法1 (數(shù)據(jù)類型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math...
    GB_speak閱讀 41,397評(píng)論 2 6
  • 第一章 初識(shí)javaJAVA 第一講:什么是程序?:為了讓計(jì)算機(jī)執(zhí)行某些操作或解決某個(gè)問題而編寫的一系列有序指令的...
    人子日月幾點(diǎn)閱讀 592評(píng)論 0 1
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個(gè)月起每個(gè)月都生一對兔子,小兔子長到第三個(gè)月后每個(gè)月又生一對兔...
    葉總韓閱讀 5,223評(píng)論 0 41
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個(gè)月起每個(gè)月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 2,070評(píng)論 0 2
  • 1 順序語句 語句:使用分號(hào)分隔的代碼稱作為一個(gè)語句。 注意:沒有寫任何代碼只是一個(gè)分號(hào)的時(shí)候,也是一條語句,...
    哈哈哎呦喂閱讀 455評(píng)論 0 0

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