public static void main(String[] args){
double a=12.81;
int b=(int)a;
System.out.println("强制类型转换:"+b);// 12
long c=Math.round(a);//调用round方法,四舍五入
System.out.println("四舍五入:"+c);//13
double d=Math.floor(a);//调用floor方法,返回小于参数的最大整数
System.out.println("floor:"+d);//12
double e=Math.ceil(a);//调用ceil方法,返回大于参数的最小整数
System.out.println("ceil:"+e);
double x=Math.random();//调用random方法,产生[0.1]之间的随机数浮点数
System.out.println("随机数:"+x);
int y=(int)(Math.random()*99);
System.out.println("产生[0,99]之间的随机整数:"+y);
int[] nums = new int[10];// 定义一个整型数组,长度为10
for (int i = 0; i < nums.length; i++) { //通过循环给数组赋值
int z = (int)(Math.random()*10);// 产生10以内的随机数
nums[i] = z;// 为元素赋值
}
for (int num:nums) {// 使用foreach循环输出数组中的元素
System.out.print(num + " ");
}
System.out.println("最大值:"+(Math.max(10, 20)));
System.out.println("生成随机数 :"+Math.round((Math.random()*98)));
}
Math类的使用(Math一个有关数学的类)
猜你喜欢
转载自blog.csdn.net/qq_40694145/article/details/81411232
今日推荐
周排行