我在制作这个方法时遇到了一些麻烦,我认为我做得对,但显然我不是,我正在研究我的 changeLight() 方法,但我的 if 语句出现了红线,我我不知道为什么。该方法应该查看 currentState 并更改它,如果 currentState 是 State.GO 那么它应该更改为 State.WARN,如果 currentState 是 State.Warn 那么它应该更改为 State.STOP,如果当前状态是 State。 STOP 然后它应该更改为 State.GO。这只是一个简单的红绿灯程序。
这里需要一点帮助,不胜感激,谢谢。
这是我的代码。
package trafficlight;
import java.awt.Color;
public class TrafficLight {
private int goDuration;
private int stopDuration;
private int warnDuration;
public enum State {STOP, GO, WARN};
public Color GO_COLOR = Color.green;
public Color STOP_COLOR = Color.red;
public Color OFF_COLOR = Color.darkGray;
public Color WARNING_COLOR = Color.yellow;
private State currentState;
public static void main(String[] args) {
}
public TrafficLight(int goDuration, int stopDuration, int warnDuration) {
this.goDuration = goDuration = 2;
this.stopDuration = stopDuration = 2;
this.warnDuration = warnDuration = 1;
this.currentState = currentState = State.GO;
}
public static void changeLight() {
if (currentState = State.GO) {
currentState = State.WARN;
}
}
public int getGoDuration() {
return goDuration;
}
public void setGoDuration(int goDuration) {
this.goDuration = goDuration;
}
public int getStopDuration() {
return stopDuration;
}
public void setStopDuration(int stopDuration) {
this.stopDuration = stopDuration;
}
public int getWarnDuration() {
return warnDuration;
}
public void setWarnDuration(int warnDuration) {
this.warnDuration = warnDuration;
}
public State getCurrentState() {
return currentState;
}
public void setCurrentState(State currentState) {
this.currentState = currentState;
}
}