怎么得到随机数~~~在线等`~~
JSP里产生了随机数,我想赋值给一个文本框,然后做判断,然后根据值在页面输出各种不同的情况~~
------解决方案--------------------随机数生成器:
package edu.zsu.zouang.util;
import java.util.Random;
public class Randomizer {
private int lower;
private int upper;
private static Random random = new Random();
public Randomizer(int lower, int upper){
if(upper <= lower){
throw new
IllegalStateException( "Upper is smaller than lower! ");
}
this.lower = lower;
this.upper = upper;
}
public Double nextDouble(){
return Double.valueOf(lower + (upper - lower) * random.nextDouble());
}
public Integer nextInteger(){
return Integer.valueOf(lower +random.nextInt(upper - lower));
}
public char[] nextBitArray(int length){
if(length <= 0){
throw new IllegalStateException( "Length is less than ZERO! ");
}
char[] temp = new char[length];
for(int i = 0; i < length ; i++){
temp[i] = random.nextBoolean() ? '1 ' : '0 ';
}
return temp;
}
}
------解决方案--------------------楼上正解,
Random类,顶1下,定义成Static的得了
至于赋值给给text框,直接
<%int(double) xxxxx = Randomizer.nexInt().intValue();%>
<input type=text,value= " <%=xxxxx%> ">
试试可以吗
------解决方案--------------------Rick_ang(东方未名) 的答案正确
这里就不写了
------解决方案--------------------new Random().nextInt();
或者用apache-commons-lang
里面有一个随机类
RandomUtils,非常方便
------解决方案--------------------Random类
------解决方案--------------------up