简体   繁体   English

java错误stackoverflow

[英]java error stackoverflow

I want to generate random numbers within a range and the generated numbers should not collide within some range. 我想生成一个范围内的随机数,并且生成的数字不应在某个范围内冲突。
i used following code but im getting Stackoverflow error.. Any better solution? 我用下面的代码,但我得到Stackoverflow错误..任何更好的解决方案?

static int [] xPositions=new int[10];
int WIDTH=700
public static void main(String args[])throws IOException
{

    if(generateRandomXPositions(10)){
        for(int i=0;i<10;i++){
          System.out.println(" Random Numbers "+i+"  :"+xPositions[i]);
        }
    }


}

private static boolean generateRandomXPositions(int n) {

    for(int i=0;i<10;i++){
        int temp=((int)0 + (int)(Math.random() * ((WIDTH - 0) + 1)));
        for(int j=0;j<xPositions.length;j++){
            if(xPositions[j]>temp-50 && xPositions[j]<temp+50){ // IF GENERATED NUMBER IS IN THIS RANGE IT SHOULD REGENERATE THE NUMBERS 
                generateRandomXPositions(10); 
            }
        }
        xPositions[i]=temp;
    }
    return true;
}

I know problem is here 我知道问题在这里

if(xPositions[j]>temp-50 && xPositions[j]<temp+50).   

Below one works fine 下一个工作正常

`if(xPositions[j]==temp)`.  

But I need that random numbers should follow that range! 但我需要随机数应遵循该范围! .

Many are wondering about the exit condition of recursive loop. 许多人想知道递归循环的退出条件。 But I believe if random number is not in that range, then there is no point of entering in to the recursive loop. 但是我相信,如果随机数不在该范围内,那么就没有必要进入递归循环。

UPDATE 1: 更新1:

And I believe compiler is tired to find the number between this range! 而且我相信编译器会很累地找到这个范围之间的数字! Now I found that it is impossible fit 10 images having width of 100px each in to the 700px width container without colliding X positions! 现在,我发现不可能将10个宽度均为100px的图像装入700px宽度的容器中而不会碰撞X位置!

Please see the image below. 请参见下图。 Lets imagine i want to place this boxes randomly without colliding... how can i do that? 假设我想将这个盒子随机放置而不发生碰撞...我该怎么做?

在此处输入图片说明

Since I think this is homework and your question is pretty vague, try to fill in these methods yourself and combine them intelligent. 由于我认为这是家庭作业,并且您的问题很模糊,因此请尝试自己填写这些方法并将它们巧妙地结合起来。

public int generateRandom();
public boolean isAccepted(int number);
public void generate();

For generate() , use some loop like: 对于generate() ,请使用类似以下的循环:

int temp = generateRandom();
while (!isAccepted(temp)) temp = generateRandom();

You're getting a StackOverflowError because the chances of xPositions[j]>temp-50 && xPositions[j]<temp+50 not passing is very low when there's a range of 50. The chances of this function terminating is even lower due to the inner for-loop. 之所以收到StackOverflowError是因为当范围为50时xPositions[j]>temp-50 && xPositions[j]<temp+50不通过的可能性非常低。由于以下原因,该函数终止的可能性甚至更低内部for循环。 Thus, this will keep recursing.. 因此,这将继续递归。

However, it doesn't seem like you're doing what you actually want to accomplish. 但是,您似乎并没有在做您真正想要完成的事情。 If you want to generate numbers that are all within a set range, you don't want to compare xPositions[j] to temp-50 and temp+50 . 如果要生成均在设定范围内的数字,则不想将xPositions[j]temp-50temp+50 That's going to regenerate numbers when xPositions[j] isn't within some random range. xPositions[j]不在某个随机范围内时,这将重新生成数字。


If you really just want to generate numbers that are within a certain range, then you'll want to get rid of the inner for-loop and instead do something like this: 如果您真的只想生成一定范围内的数字,那么您将希望摆脱内部for循环,而应执行以下操作:

for every number in xPositions:
  a = random number
  if a is within an unwanted range, regenerate numbers
  else set xPositions[i] = a

or without recursion: 或没有递归:

for every number in xPositions:
  a = random number
  while a is within an unwanted range:
    a = random number
  set xPositions[i] = a

On the other hand, if you want to randomize the order of some images along the x-axis, you can do something like this: 另一方面,如果要沿x轴随机分配某些图像的顺序,则可以执行以下操作:

bag = [0 1 2 ... n-1]
shuffle bag
for every number in xPositions:
  xPositions[i] = bag.pop * IMAGE_WIDTH

You've committed the classic error when writing recursive methods: no stopping condition. 在编写递归方法时,您犯了经典错误:没有停止条件。

Your method has to have one condition that returns false. 您的方法必须具有一个返回false的条件。

Instead of calling generateRandomXPositions() again you should use a loop for creating a number and checking if it already exists or not. 而不是再次调用generateRandomXPositions() ,您应该使用循环来创建数字并检查数字是否已经存在。

The stackOverFlowError occurs because you recursively call the function from within itself. 发生stackOverFlowError是因为您从自身内部递归调用了该函数。

You called generateRandomXPositions(10); 您调用了generateRandomXPositions(10); inside a loop? 循环内? This is inifinite loop, man. 这是无限循环,伙计。 And you never use the param from the method. 而且您永远不会使用方法中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM