最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

java Math類和Random類的用法

2022-07-13 20:29 作者:虛云幻仙  | 我要投稿

/**
* 測試Math類的常用方法
* 測試Random類
*/

public class TestMath {
? ?public static void main(String[] args) {
? ? ? ?int a = 4;
? ? ? ?int b = -5;
? ? ? ?int c = 2;
? ? ? ?double d = 1.1;
? ? ? ?double e = 1.5;
? ? ? ?System.out.println(Math.abs(b)+"Math.abs()取絕對值 結(jié)果為5 返回結(jié)果為同數(shù)據(jù)類型");
? ? ? ?System.out.println(Math.sqrt(a)+"Math.sqrt()取平方根 4開平方結(jié)果為2.0 返回double");
? ? ? ?System.out.println(Math.pow(c,a)+"Math.pow()冪運(yùn)算 返回c的a次方 結(jié)果為16.0 返回double");
? ? ? ?System.out.println(Math.max(a,c)+"Math.max()取兩數(shù)最大值 結(jié)果為4 返回同數(shù)據(jù)類型");
? ? ? ?System.out.println(Math.min(a,c)+"Math.min()取兩數(shù)最小值 結(jié)果為2 返回同數(shù)據(jù)類型");
? ? ? ?System.out.println(Math.ceil(d)+"Math.ceil()取大于d的最小整數(shù) 結(jié)果為2.0 返回double");
? ? ? ?System.out.println(Math.floor(e)+"Math.floor()取小于e的最大整數(shù) 結(jié)果為1.0 返回double");
? ? ? ?System.out.println(Math.random()+"取[0,1)的隨機(jī)數(shù) 返回double 范圍包含0不包含1");
? ? ? ?System.out.println(Math.round(e)+"Math.round()四舍五入 結(jié)果為2 返回long");
? ? ? ?System.out.println(Math.toDegrees(Math.PI)+"Math.toDegrees()將弧度轉(zhuǎn)為角度 PI對應(yīng)3.14 結(jié)果為180.0 返回double");
? ? ? ?System.out.println(Math.toRadians(45)+"Math.toRadians()將角度轉(zhuǎn)為弧度 45度等于PI/4 結(jié)果為0.7853981633974483 返回double");
? ? ? ?System.out.println(Math.sin(Math.toRadians(30))+"Math.sin()求弧度的sin值 對邊除以斜邊 用Math.toRadians將30度轉(zhuǎn)換為弧度 結(jié)果為0.49999999999999994 返回double");
? ? ? ?System.out.println(Math.cos(Math.PI/4)+"Math.cos()求弧度的cos值 鄰邊除以斜邊 PI/4是45度 結(jié)果為0.7071067811865476 返回double");
? ? ? ?System.out.println(Math.tan(Math.PI/4)+"Math.tan()求弧度的tan值 對邊除以鄰邊 結(jié)果為0.9999999999999999 返回double");
? ? ? ?System.out.println(Math.toDegrees(Math.asin(0.5))+"Math.asin()求arcsin值 通過已知sin值求弧度 返回double弧度 加toDegrees()轉(zhuǎn)為角度 結(jié)果為30.000000000000004");
? ? ? ?System.out.println(Math.toDegrees(Math.acos(1/Math.sqrt(2)))+"Math.acos()求arccos值 返回double弧度 1除以根號2是45度 結(jié)果為45.00000000000001");
? ? ? ?System.out.println(Math.atan(3.0/4)+"Math.atan()求arctan值 返回double弧度 結(jié)果為0.6435011087932844");

? ? ? ?Random ran = new Random();
? ? ? ?//Random類 用于產(chǎn)生隨機(jī)數(shù)
? ? ? ?System.out.println(ran.nextDouble()+"隨機(jī)生成[0,1)的小數(shù) 作用同Math.random");
? ? ? ?System.out.println(ran.nextInt()+"隨機(jī)生成int值 正負(fù)21億之間隨機(jī)取");
? ? ? ?System.out.println(ran.nextBoolean()+"隨機(jī)生成true或者false");
? ? ? ?System.out.println(ran.nextInt(10)+"隨機(jī)生成[0,10)區(qū)間的整數(shù) 不包含10 隨機(jī)0-9");
? ? ? ?System.out.println(20+ran.nextInt(6)+"隨機(jī)生成20-25的整數(shù) .nextInt(6)是0-5 +20變成20-25");
? ?}
}

java Math類和Random類的用法的評論 (共 條)

分享到微博請遵守國家法律
宽甸| 靖边县| 葵青区| 哈巴河县| 郸城县| 龙门县| 会东县| 凤山市| 雷波县| 布拖县| 五家渠市| 六盘水市| 武定县| 棋牌| 桂平市| 万载县| 盐池县| 商水县| 巴林右旗| 平陆县| 抚宁县| 东海县| 柳河县| 宜章县| 保德县| 和林格尔县| 白朗县| 句容市| 班玛县| 津市市| 唐山市| 洛阳市| 卢龙县| 西华县| 汝南县| 天祝| 松阳县| 黄浦区| 南丹县| 古蔺县| 余姚市|