java错误堆栈溢出

发布于 2024-12-07 21:32:06 字数 1374 浏览 0 评论 0 原文

我想生成一个范围内的随机数,并且生成的数字不应在某个范围内发生冲突。
我使用了以下代码,但我收到 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;
}

我知道问题就在这里

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

下面一个工作正常

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

但我需要随机数应该遵循这个范围! 。

许多人想知道递归循环的退出条件。但我相信如果随机数不在这个范围内,那么就没有必要进入递归循环。

更新 1:

我相信编译器厌倦了找到这个范围之间的数字!现在我发现不可能将 10 个宽度为 100px 的图像放入 700px 宽度的容器中而不碰撞 X 位置!

请看下图。让我们想象一下我想随机放置这个盒子而不发生碰撞......我该怎么做?

在此处输入图像描述

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?

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:

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!

Please see the image below. Lets imagine i want to place this boxes randomly without colliding... how can i do that?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤君无依 2024-12-14 21:32:06

因为我认为这是作业,而且你的问题相当模糊,所以尝试自己填写这些方法并将它们巧妙地结合起来。

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

对于generate(),使用一些循环,例如:

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

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:

int temp = generateRandom();
while (!isAccepted(temp)) temp = generateRandom();
爱要勇敢去追 2024-12-14 21:32:06

您会收到 StackOverflowError 因为 xPositions[j]>temp-50 && 的机会当范围为 50 时,xPositions[j] 不通过的可能性非常低。由于内部 for 循环,该函数终止的可能性甚至更低。因此,这将继续递归。

但是,您似乎并没有在做您真正想要完成的事情。如果您想要生成全部在设定范围内的数字,则不希望将 xPositions[j]temp-50temp+50 进行比较。当 xPositions[j] 不在某个随机范围内时,这将重新生成数字。


如果您真的只想生成特定范围内的数字,那么您将需要摆脱内部 for 循环,而是执行如下操作:

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

或不使用递归:

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

另一方面,如果您想要随机化一些图像沿x轴的顺序,你可以这样做:

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

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. 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. That's going to regenerate numbers when xPositions[j] isn't within some random range.


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 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:

bag = [0 1 2 ... n-1]
shuffle bag
for every number in xPositions:
  xPositions[i] = bag.pop * IMAGE_WIDTH
牵强ㄟ 2024-12-14 21:32:06

在编写递归方法时,您犯了一个经典错误:没有停止条件。

您的方法必须有一个返回 false 的条件。

You've committed the classic error when writing recursive methods: no stopping condition.

Your method has to have one condition that returns false.

陌伤ぢ 2024-12-14 21:32:06

您应该使用循环来创建数字并检查它是否已存在,而不是再次调用generateRandomXPositions()

发生 stackOverFlowError 是因为您从函数内部递归调用该函数。

Instead of calling generateRandomXPositions() again you should use a loop for creating a number and checking if it already exists or not.

The stackOverFlowError occurs because you recursively call the function from within itself.

∞觅青森が 2024-12-14 21:32:06

您调用了generateRandomXPositions(10);在循环内?这是无限循环,伙计。并且您永远不会使用该方法中的参数。

You called generateRandomXPositions(10); inside a loop? This is inifinite loop, man. And you never use the param from the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文