【问题标题】:How do i fix my program not responding?如何修复我的程序没有响应?
【发布时间】:2014-09-06 19:07:09
【问题描述】:

我的程序在生成地形时启动程序一直没有响应。

这是代码。我相信这是 for 循环。

Random Random = new Random();
        int numberHeight = Random.nextInt(5);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glColor3f(0.5f,0.5f,1.0f);
        int Bottom = Height - 100;
        for(int xOffset = 0; xOffset < Width; xOffset = xOffset + 100){
            for(int currentHeight = 0; currentHeight < numberHeight; currentHeight = currentHeight++){
                GL11.glBegin(GL11.GL_QUADS);
                {
                    GL11.glVertex2f(xOffset,Bottom - currentHeight * 100);
                    GL11.glVertex2f(xOffset + 100,Bottom - currentHeight * 100);
                    GL11.glVertex2f(xOffset + 100,Bottom - currentHeight * 100 - 100);
                    GL11.glVertex2f(xOffset,Bottom - currentHeight * 100 - 100);
                }
                GL11.glEnd();
                if(currentHeight >= numberHeight)break;
            }
        }

【问题讨论】:

  • 你得到什么样的异常?在哪一行?
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码在问题本身。没有明确的问题陈述的问题对其他读者没有用处。见How to create a Minimal, Complete, and Verifiable example
  • 我认为问题一定是for循环可能使多维数据集超出范围。

标签: java random 2d lwjgl terrain


【解决方案1】:

问题是for循环没有写正确:

我试过这段代码:

public static void main(String []args){
   for(int i = 0;i < 10;i = i++)
   {
       System.out.println(i);
   }
}

无限循环,一直打印0。

你的循环是:

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight = currentHeight++)

应该是

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight++)

或:

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight += 1)

递增/递减运算符如何用作前缀或后缀:

Java: Prefix/postfix of increment/decrement operators?

【讨论】:

  • 我将它更改为 CurrentHeight++ 并且它工作但它最终在每个块中制作相同大小的块,直到它可能是因为这两个 for 循环是它的一部分导致它这样做那。但无论如何感谢这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多