布尔标志不能读取while循环(JAVA) [英] boolean flag not read in while loop (java)

查看:184
本文介绍了布尔标志不能读取while循环(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序来反弹,屏幕一球。下面写不工作的程序(皮球稍稍移开屏幕)。

但是,如果我宣布while循环中的布尔变量atHorizo​​ntalEdge和atVerticalEdge,它似乎工作。为什么会这样呢?由于布尔为整个run()方法的定义,应该不会是被调用whil​​e循环,即使其在while循环外?

 进口acm.program *。
进口acm.graphics *。
进口java.awt中的*。公共类BouncingBallv3扩展GraphicsProgram {
    公共无效的run(){        双X =(的getWidth() - BALL_SIZE)/ 2; //设置球的中锋首发位置
        双Y =(的getHeight() - BALL_SIZE)/ 2;
        GOval球=新GOval(X,Y,BALL_SIZE,BALL_SIZE); //在屏幕中心创建了一个红球
        ball.setFilled(真);
        ball.setColor(Color.red);
        加(球);        双DX = 1; //增量通过该球移动
        双DY = 1;        //声明布尔变量来测试,如果球的位置是在边缘
        布尔atHorizo​​ntalEdge =(ball.getX()==的getWidth() - BALL_SIZE)|| ball.getX()== 0;
        布尔atVerticalEdge =(ball.getY()==的getHeight() - BALL_SIZE)|| ball.getY()== 0;        / * while循环保持球的方向移动DX,DY
         *如果球到达任何边缘DY变化的位置,方向或DX
         * /        而(真){            如果(atHorizo​​ntalEdge){//如果它击中左/右墙球改变方向
                DX = -dx;
            }否则如果(atVerticalEdge){//如果它击中一顶/底墙球改变方向
                DY = -dy;
            }
                ball.move(DX,DY);
                暂停(PAUSE_TIME);        }    }    私有静态最后双BALL_SIZE = 50;
    私有静态最终诠释PAUSE_TIME = 5;
}


解决方案

问题不在于布尔的声明是while循环之外。这是你检查while循环之外的boundraies。正因为如此,你的条件从不更新和只检查为球的原始状态。

I've written a program to bounce a ball around a screen. The program as written below does not work (the ball just moves off screen).

However if I declare the boolean variables atHorizontalEdge and atVerticalEdge inside the while loop, it seems to work. Why is that the case? As the booleans are defined for the entire run() method, shouldn't it be callable by the while loop even though its outside the while loop?

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;
}

解决方案

The issue is not that the declaration of the booleans is outside the while loop. It is that you are checking for your boundraies outside the while loop. Because of this, your condition is never updated and it only checks for the original state of the ball.

这篇关于布尔标志不能读取while循环(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆