我正在开发Android Studio中的Asteroid道奇游戏。我使用java中的in random
生成器为每个小行星创建随机位置。我一直收到java.lang.IllegalArgumentException: n must be positive
,但它在我的手机上仍能正常运行。
例外:
java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:300)
at ca.alnitakstudios.Asteroids.Asteroids.<init>(Asteroids.java:38)
at ca.alnitakstudios.Screens.GameScreen.<init>(GameScreen.java:36)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
at android.view.LayoutInflater.inflate(LayoutInflater.java:381)
代码:
public class Asteroids {
private Random random;
private int amountOfAsteroids, fastestSpeed, slowestSpeed,
biggest, smallest;
private int[] x, y, speeds, radii;
public Asteroids() {
amountOfAsteroids = 5;
x = new int[amountOfAsteroids];
y = new int[amountOfAsteroids];
speeds = new int[amountOfAsteroids];
radii = new int[amountOfAsteroids];
fastestSpeed = 7;
slowestSpeed = 3;
biggest = 20;
smallest = 10;
random = new Random();
for (int i = 0; i < amountOfAsteroids; i++) {
//init x & y placement
x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
y[i] = random.nextInt(GameScreen.height);
//Setting speeds
speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;
//Setting the radii
radii[i] = random.nextInt(biggest - smallest) + smallest;
}
}
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GRAY);
for(int i = 0; i < amountOfAsteroids; i++) {
x[i] -= speeds[i];
canvas.drawCircle(x[i], y[i], radii[i], paint);
}
checkIfOutOfBounds();
}
public void checkIfOutOfBounds() {
for(int i = 0; i < amountOfAsteroids; i++) {
if(x[i] < 0) {
//x & y placement after its been out of bounds
x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
y[i] = random.nextInt(GameScreen.height);
//Setting new speeds
speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;
//Setting new radii
radii[i] = random.nextInt(biggest - smallest) + smallest;
}
}
}
}
答案 0 :(得分:2)
n must be positive at java.util.Random.nextInt
您向nextInt
方法传递了一个否定值,确保在扣除fastestSpeed - slowestSpeed
和biggest - smallest
时它不是负数。或者您也可以使用Math.abs将所有负数都设为正数
<强>样品:强>
Math.abs(fastestSpeed - slowestSpeed)
答案 1 :(得分:0)
查看堆栈跟踪,似乎问题可能是,因为您在初始化{{1}期间尝试使用GameScreen
的静态成员}}。这些静态成员是否正确初始化为正值?如果它们被初始化为GameScreen
,这将是一个问题。
由于你非常肯定它应该总是正面的,但不知怎的,你仍然得到零或负值,我建议你创建一个虚拟 0
类来进行调试。
Random
这应该覆盖package ca.alnitakstudios.Asteroids;
public class Random {
private java.util.Random r = new java.util.Random();
public int nextInt(int i) {
//Debugging (enable if you need to trace)
//new Exception().printStackTrace();
//(or you can also:)
//StackTraceElement ste = new Exception().getStackTrace()[1]; //the caller
//print out ste.getClassName(), ste.getMethodName(), ste.getLineNumber() etc.
System.err.println("nextInt called with value " + i);
return r.nextInt(i);
}
}
类中使用的Random
类,否则您可以明确导入它。不需要更改其他代码,因此您肯定知道您不会无意中修改任何内容。