黑马程序员——Java基础——其他对象(Math-Random)

来源:互联网 发布:mac转换输入法快捷键 编辑:程序博客网 时间:2024/05/16 18:11
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


import java.util.Random;/** * Math 类: * 包含基本的数字操作,如指数、对数、平方根和三角函数。 */public class MathDemo {public static void main(String[] args) {//该类中常用的部分方法://1.ceil返回大于指定数的最小整数double d1 = Math.ceil(12.3);System.out.println("d1="+d1);//2.floor返回小于指定数的最大整数double d2 = Math.floor(12.3);System.out.println("d2="+d2);//3.四舍五入double d3 = Math.round(12.4);double d4 = Math.round(12.5);System.out.println("d3="+d3);System.out.println("d4="+d4);//4.pow(double a,double b);一个数自乘若干次。 参数:a底数;b指数;//如下:求2的3次幂double d5 = Math.pow(2, 3);System.out.println("d5="+d5);/** * 以下为输出结果:  d1=13.0d2=12.0d3=12.0d4=13.0d5=8.0 *///5.random();随机数 返回带正号的 double 值,大于或等于 0.0,小于 1.0。返回值是一个伪随机数(有规律可寻)//返回1到10 的随机数://方法一for(int i=0;i<10;i++){int num = (int) (Math.random()*10+1);System.out.print(num+" ");}System.out.println();//方法二://调用Random对象中的nextInt(int n)方法 参数:n - 所返回随机数的范围//返回一个伪随机数,处于 0(包括)和 n(包括)之间均匀分布的 int 值。Random random = new Random();for(int i=0;i<10;i++){int num = random.nextInt(10)+1;System.out.print(num+" ");}}}


0 0
原创粉丝点击