您的位置:首页 > 职场人生

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

2014-09-19 16:06 459 查看
------- <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.0
d2=12.0
d3=12.0
d4=13.0
d5=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+" ");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: