我基本上必须编写交通信号灯以红色开始,然后红色开绿色并再次返回红色。我设法让交通信号灯从红色闪烁变为绿色但反之亦然,有人可以查看我的代码,看看我在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) {
}
}
}
答案 0 :(得分:0)
/** * 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) {
}
}
}