【发布时间】:2015-06-21 11:53:54
【问题描述】:
所以,我确实重新做了e=(high-low)*x+low,但是,当我尝试使用它时,它只是返回了一个 outOfBounds 异常。当我没有将它转换为 int 时,它可以工作,但是我需要它返回一个整数,以便数组工作。
public static int randomInt(int low, int high)
{double e;
double x=Math.random();
e=(high-low)*x+low;
return (int)e;}
这里是调用“randomInt”方法的方法
public static int[] randomIntArray(int n)//generates an array of random numbers based on an upper and lower bound
{int[] a=new int[n];
for(int i=0;i<a.length;i++)
{a[i]=randomInt(-5,15);}//"-5&15 are the lower and upper bounds of the random number array
return a;}
【问题讨论】:
-
为什么不使用java.Util.Random#nextInt()?
-
这不应该抛出 OutOfBoundsException。
-
事实证明,OutOfBoundsException 不是来自我必须打印的打印方法。