这个方法有些麻烦

时间:2013-11-13 19:04:02

标签: java

我在制作这个方法时遇到了一些麻烦,我认为我做得对,但显然我不是,我正在研究我的changeLight()方法,但是我的if语句上有一个红线,而且我不确定为什么。该方法应该查看currentState并更改它,如果currentState是State.GO则它应该更改为State.WARN,如果currentState是State.Warn那么它应该更改为State.STOP,如果当前状态是State。停止然后它应该改为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;
  }
}

4 个答案:

答案 0 :(得分:1)

您在if语句中使用=。这是赋值运算符。您想使用==这是相等运算符。

你得到"红线"是因为你说currentState当你打算问State.GO时,currentState等于State.GO?"


这只是众多错误中的一个。另一个错误是:

public static void changeLight();

那里你不应该有分号。你想用花括号括起后面的代码来说,"这是我方法的代码"。


当您修复时,您应该:

public static void changeLight() {
    if(currentState == State.GO){
        currentState = State.WARN;


    }
}

但这将是一个错误,因为此方法是静态的,currentState不是静态变量。您可以通过将签名更改为:

来解决此问题
    public void changeLight()

答案 1 :(得分:0)

检查你的语法。

public static void changeLight(){
  if(currentState == State.GO){
      currentState = State.WARN;
  }
}

编辑:当然==而不是=

答案 2 :(得分:0)

if(currentState == State.go){
    currentState = State.WARN;
}

=是赋值,但==是等价性测试。你正在做的是说currentState现在等于State.go(currentState = State.go)你想要的是是currentState等于State.go (currentState == State.go)

答案 3 :(得分:0)

我建议您将枚举用作Finite State machine这样的

 public enum State {
   STOP, GO, WARN;
   public Color getColor() {
    switch (this) {
    case GO: // Green 
      return Color.green;
    case WARN:
      return Color.yellow;
    }
    // I don't think you want a State for OFF.
    return Color.red; // Red
  }

   State nextLight() {
    switch (this) {
    case GO: // Green -> Yellow
      return WARN;
    case STOP: // Red -> Green
      return GO;
    }
    return STOP; // Default to Red (Yellow -> Red)
  }

 // The enum is now a FSM.
 public void changeLight(){
   currentState = currentState.nextLight();
 }
相关问题