繁体   English   中英

布尔标志未在while循环中读取(java)

[英]boolean flag not read in while loop (java)

我编写了一个程序,可以在屏幕周围弹跳球。 下面编写的程序不起作用(球只是移出屏幕)。

但是,如果我在while循环中声明布尔变量atHorizo​​ntalEdge和atVerticalEdge,它似乎可以工作。 为什么会这样? 由于布尔值是为整个run()方法定义的,即使它在while循环之外,它是否也可以由while循环调用?

import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class BouncingBallv3 extends GraphicsProgram {
    public void run() {

        double x = (getWidth() - BALL_SIZE)/2 ;  //sets the starting position of ball at center
        double y = (getHeight() - BALL_SIZE)/2 ;


        GOval ball = new GOval (x, y, BALL_SIZE, BALL_SIZE ); // creates a red ball at center of screen
        ball.setFilled(true);
        ball.setColor(Color.red);
        add (ball);

        double dx = 1; //increments by which the ball moves
        double dy = 1;

        //declares boolean variables to test if ball position is at an edge
        boolean atHorizontalEdge =  (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ; 
        boolean atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;

        /* while loop keeps the ball moving in direction dx,dy
         * if ball reaches a position at any edge, the direction dx or dy changes
         */

        while (true) {

            if (atHorizontalEdge) {          //changes direction of ball if it hits a left/right wall
                dx = -dx;
            } else if (atVerticalEdge) {     //changes direction of ball if it hits a top/bottom wall
                dy = -dy;
            }
                ball.move(dx,dy); 
                pause (PAUSE_TIME);

        }



    }



    private static final double BALL_SIZE = 50;
    private static final int PAUSE_TIME = 5;
}

问题不在于布尔值的声明在while循环之外。 这是因为您正在检查while循环之外的边界。 因此,您的状况永远不会更新,它只会检查球的原始状态。

我认为您应该在每次迭代后在循环主体中更新atHorizontalEdgeatVerticalEdge


更新:

while循环体应该是这样的,`//声明布尔变量以测试球的位置是否在边缘boolean atHorizo​​ntalEdge = false; boolean atVerticalEdge = false;

    /* while loop keeps the ball moving in direction dx,dy
     * if ball reaches a position at any edge, the direction dx or dy changes
     */

    while (true) {
        atHorizontalEdge = (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0;
        atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0;

        if (atHorizontalEdge) {          //changes direction of ball if it hits a left/right wall
            dx = -dx;
        } else if (atVerticalEdge) {     //changes direction of ball if it hits a top/bottom wall
            dy = -dy;
        }
            ball.move(dx,dy); 
            pause (PAUSE_TIME);

    }`

如果在循环内定义atHorizontalEdgeatVerticalEdge它之所以atHorizontalEdge ,是因为每次迭代都会重新计算(即更新)这两个变量。

可以在while循环内部或外部声明atHorizontalEdgeatVerticalEdge ,这并不重要。

重要的是,在循环开始之前,以下内容仅计算一次

atHorizontalEdge =  (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ; 
atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;

因此, atHorizontalEdgeatVerticalEdgerun方法的开始到结束都将具有相同的值(这是永远的)。

您显然希望上述两行在循环的每次迭代中执行,因为它们不会自行更新...

while (true) {
   atHorizontalEdge =  (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ; 
   atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;
   ...
}


编辑:另外,检查x和y是否大于或等于宽度/高度,并且小于或等于 0是一个更好的主意,这有两个原因:

  1. 如果决定将增量从1更改,则可以跳过该确切值并导致错误,但更重要的是:
  2. 您使用的是double ,并且浮点数表示的数字可能与您要比较的数字不完全相同 ,因此==可能会导致错误,并且球可能越过边缘并继续前进。

即。 ball.getX() >= getWidth() ... ball.getX() <= 0

每个计算机科学家都应了解的浮点运算法则

暂无
暂无

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

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