If-Else语句交通灯编码Java Blue J

If-Else语句交通灯编码Java Blue J,java,if-statement,bluej,statements,Java,If Statement,Bluej,Statements,我基本上必须对交通灯进行编码,从红色开始,然后从红色变为绿色,再回到红色。我设法让交通灯从红色闪烁变为绿色,但不是相反。有人能看看我的代码,看看我在if和else语句中犯了什么错误吗 /** * Write a description of class TrafficLight here. * * @author (your name) * @version (a version number or a date) */ public class Traff

我基本上必须对交通灯进行编码,从红色开始,然后从红色变为绿色,再回到红色。我设法让交通灯从红色闪烁变为绿色,但不是相反。有人能看看我的代码,看看我在if和else语句中犯了什么错误吗

    /**  * Write a description of class TrafficLight here.  *   *
    @author (your name)   * @version (a version number or a date)  */
    public class TrafficLight {
        // instance variables - replace the example below with your own
        private Circle red;
        private Circle yellow;
        private Circle green;
        private Boolean stop;

        /**
         * Constructor for objects of class TrafficLight
         */
        public TrafficLight()
        {

            red = new Circle();
            red.changeColor("red");
            red.moveHorizontal(200);
            red.moveVertical(200);
            red.changeSize(60);
            red.makeVisible();
            stop=true;
            stop=false;

            yellow = new Circle();
            yellow.changeColor("black");
            yellow.moveHorizontal(200);
            yellow.moveVertical(260);
            yellow.changeSize(60);
            yellow.makeVisible();

            green = new Circle();
            green.changeColor("black");
            green.moveHorizontal(200);
            green.moveVertical(320);
            green.changeSize(60);
            green.makeVisible();


            // Construct the circles
            // Set their color and make them visible
            // Set stop to be true;
        }

        /**
         * If the traffic light shows STOP, it changes to GO.
         * If the traffic light shows GO, it changes to STOP.
         */
        public void change() {

            // read the instructions for the user story 
            // of how this method should work

            if (stop==false) {
                red.changeColor("red");
                yellow.changeColor("yellow");
                green.changeColor("black");
                pause();
                red.changeColor("black");
                yellow.changeColor("black");
                green.changeColor("green");
                pause();

            } else{ 
                red.changeColor("black");
                yellow.changeColor("yellow");
                green.changeColor("black");
                pause();
                red.changeColor("red");
                yellow.changeColor("black");
                green.changeColor("black");
            }

            /*
             * Do not change anything in the pause method
             */

        }   

        private void pause() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }

    }

停止值始终设置为false。这就是问题所在。我可以看到您将stop初始化为true,然后再次初始化为false。尝试更改它并以正确的方式初始化它。@Shiladittyacharaborty你是什么意思?我应该如何更改它?如果(stop==false),则设置stop=true,反之亦然versa@ShiladittyaChakraborty它不起作用了检查我的答案。让我知道它是否对您有帮助。从何处调用change方法?else语句仍然不运行,因为stop值未正确初始化,这就是为什么每次stop都设置为false。我如何正确初始化它?当红灯亮起时,然后更改stop的值,并针对您拥有的另一个条件执行此操作。
/**  * Write a description of class TrafficLight here.  *   *
@author (your name)   * @version (a version number or a date)  */
public class TrafficLight {
    // instance variables - replace the example below with your own
    private Circle red;
    private Circle yellow;
    private Circle green;
    private Boolean stop = false;

    /**
     * Constructor for objects of class TrafficLight
     */
    public TrafficLight()
    {

        red = new Circle();
        red.changeColor("red");
        red.moveHorizontal(200);
        red.moveVertical(200);
        red.changeSize(60);
        red.makeVisible();

        yellow = new Circle();
        yellow.changeColor("black");
        yellow.moveHorizontal(200);
        yellow.moveVertical(260);
        yellow.changeSize(60);
        yellow.makeVisible();

        green = new Circle();
        green.changeColor("black");
        green.moveHorizontal(200);
        green.moveVertical(320);
        green.changeSize(60);
        green.makeVisible();


        // Construct the circles
        // Set their color and make them visible
        // Set stop to be true;
    }

    /**
     * If the traffic light shows STOP, it changes to GO.
     * If the traffic light shows GO, it changes to STOP.
     */
    public void change() {

        // read the instructions for the user story 
        // of how this method should work

        if (stop==false) {
            red.changeColor("red");
            yellow.changeColor("yellow");
            green.changeColor("black");
            pause();
            red.changeColor("black");
            yellow.changeColor("black");
            green.changeColor("green");
            pause();
            stop = true;

        } else{ 
            red.changeColor("black");
            yellow.changeColor("yellow");
            green.changeColor("black");
            pause();
            red.changeColor("red");
            yellow.changeColor("black");
            green.changeColor("black");
            stop = false;
        }

        /*
         * Do not change anything in the pause method
         */

    }   

    private void pause() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
    }

}