不停在xcoördinate

时间:2022-09-07 20:17:25

I am making something with a block that moves, until it hits the x=11 coördinate. I tried doing a while loop within a while loop:

我正在制作一个移动的块,直到它击中x =11coördinate。我尝试在while循环中执行while循环:

public void run() {
    x = 200;
    y = 200;
    while (true) {
        if (left == true) {
            x -= 5;
            while (true) {
                if (x == 11) {
                    left = false;
                }
            }

        }
        if (up == true) {
            y -= 5;
        }
        if (right == true) {
            x += 5;
        }
        if (down == true) {
            y += 5;
        }
        repaint();
        try {
            Thread.sleep(20);
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

But it doesn't stop at x = 11 it continues to run indefinitely. Does anyone know how else i could do this?

但它并没有停留在x = 11,而是继续无限期地运行。有谁知道我怎么能做到这一点?

3 个解决方案

#1


1  

It sounds like you want your block not to move below x = 11. In that case try:

听起来你希望你的块不要移动到x = 11以下。在这种情况下尝试:

    if (left == true) {
        if(x - 5 < 11){
            x = 11;
        }
        else{
            x -= 5;
        }
    }

#2


1  

Any program with

任何程序

while(true){
     //any code that coesn't contain break
}

will run forever

将永远运行

Each time round the loop it will execute the code in the loop, then return to the line while(true), if the condition is still true (which it clearly will be, being true it will go back around again.

每次循环循环,它将执行循环中的代码,然后返回到行(while),如果条件仍然为真(它显然将是真实的,它将再次返回。

These sorts of loop (occasionally) have legitimate usages but must contain break; somewhere in the loop to exit them.

这些类型的循环(偶尔)具有合法的用法但必须包含中断;在循环中的某个地方退出它们。

In your case both loops have this problem but the inner loop is the one it actually gets stuck in

在你的情况下,两个循环都有这个问题,但内循环是它实际陷入的循环

  while (true) {
     if (x == 11) {
         left = false;
     }
  }

Stepping though it:

踩到它:

  1. Is true==true --> yes, enter loop
  2. 是true == true - >是,输入循环

  3. Is x == 11 --> lets say yes so;
  4. 是x == 11 - >让我们这样说;

  5. set left to false;
  6. 设置为false;

  7. Is true==true --> yes, enter loop
  8. 是true == true - >是,输入循环

  9. Is x == 11 --> lets say yes so;
  10. 是x == 11 - >让我们这样说;

  11. set left to false;
  12. 设置为false;

  13. Is true==true --> yes, enter loop
  14. 是true == true - >是,输入循环

And round and round again

又一次又一次

You've stated that you want something to move down (by 5) unless its below 11, the following will achieve this

你已经声明你想要一些东西向下移动(5),除非它低于11,以下将实现这一点

while (x>11){
    x-=5;
    if (x<11){
       x==11;
    }
}

This is somewhat inefficient but I’m assuming its a simplification of a more complex problem

这有点低效,但我假设它是一个更复杂的问题的简化

#3


0  

Try

    boolean isValid=false;

    while(!isValid){
      if(left==true)
       {
        x -= 5;
          if(x ==11)
           {
            left=false;
            isValid=true;
           }
       }
   }

If this is the case not knowing your conditions

如果是这种情况不知道你的条件

#1


1  

It sounds like you want your block not to move below x = 11. In that case try:

听起来你希望你的块不要移动到x = 11以下。在这种情况下尝试:

    if (left == true) {
        if(x - 5 < 11){
            x = 11;
        }
        else{
            x -= 5;
        }
    }

#2


1  

Any program with

任何程序

while(true){
     //any code that coesn't contain break
}

will run forever

将永远运行

Each time round the loop it will execute the code in the loop, then return to the line while(true), if the condition is still true (which it clearly will be, being true it will go back around again.

每次循环循环,它将执行循环中的代码,然后返回到行(while),如果条件仍然为真(它显然将是真实的,它将再次返回。

These sorts of loop (occasionally) have legitimate usages but must contain break; somewhere in the loop to exit them.

这些类型的循环(偶尔)具有合法的用法但必须包含中断;在循环中的某个地方退出它们。

In your case both loops have this problem but the inner loop is the one it actually gets stuck in

在你的情况下,两个循环都有这个问题,但内循环是它实际陷入的循环

  while (true) {
     if (x == 11) {
         left = false;
     }
  }

Stepping though it:

踩到它:

  1. Is true==true --> yes, enter loop
  2. 是true == true - >是,输入循环

  3. Is x == 11 --> lets say yes so;
  4. 是x == 11 - >让我们这样说;

  5. set left to false;
  6. 设置为false;

  7. Is true==true --> yes, enter loop
  8. 是true == true - >是,输入循环

  9. Is x == 11 --> lets say yes so;
  10. 是x == 11 - >让我们这样说;

  11. set left to false;
  12. 设置为false;

  13. Is true==true --> yes, enter loop
  14. 是true == true - >是,输入循环

And round and round again

又一次又一次

You've stated that you want something to move down (by 5) unless its below 11, the following will achieve this

你已经声明你想要一些东西向下移动(5),除非它低于11,以下将实现这一点

while (x>11){
    x-=5;
    if (x<11){
       x==11;
    }
}

This is somewhat inefficient but I’m assuming its a simplification of a more complex problem

这有点低效,但我假设它是一个更复杂的问题的简化

#3


0  

Try

    boolean isValid=false;

    while(!isValid){
      if(left==true)
       {
        x -= 5;
          if(x ==11)
           {
            left=false;
            isValid=true;
           }
       }
   }

If this is the case not knowing your conditions

如果是这种情况不知道你的条件