Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数.)其实返回值就是该数的整数位:Math.floor(0.666) --> 0Math.floor(39.2783) --> 39 所以我们可以使用Math.floor(Math.random())去获取你想要的一
package com.summer.cn; /** * @author Summer *随机产生1-12的整数 , 根据产生整数输出一下该月份的季节信息 */ public class Test041510 { public static void main(String[] args) { //产生一个1——12的数字 int i = (int) ((Math.random()*12)+1); //通过判断语句确定随机输入的数字对应的季节 if(i>=3&&i<=6){ S
Java中Math类的random()方法可以生成[0,1)之间的随机浮点数.而double类型数据强制转换成int类型,整数部分赋值给int类型变量,小数点之后的小数部分将会丢失. 如果要生成[0,n]的随机整数的话,只需要Math.random()乘以n+1,生成[0,n+1)的浮点数,再强制类型转换为int类型,只取其整数部分,即可得到[0,n]的整数.int b=(int)(Math.random()*10);//生成[0,9]之间的随机整数. int temp=m+(int)(Math
package com.test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.Integer; /** * Created by Administrator on 2016/11/13. */ public class ClazzTest { public static void main(String [] args){ sort(5); } //用Math.rando
public class Test2 { public static void main(String args[]){ int num; int count[]=new int[21]; for(int i=0;i<10000;i++){ num=(int)(Math.random()*20+0.5); //产生0到20的随机数 count[num]++; //若产生随机数是0,则用count[0]表示它的个数,数组的初始值都为0 System.out.print(num+" "