0

このメソッドを作成するのに問題があります。正しくやっていると思っていましたが、明らかにそうではありません。changeLight() メソッドに取り組んでいますが、if ステートメントに赤線が引かれています。なぜかわからない。メソッドは currentState を調べて変更する必要があります。currentState が State.GO の場合は State.WARN に変更し、currentState が State.Warn の場合は State.STOP に変更し、現在の状態が State の場合は State.STOP に変更する必要があります。 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;
  }
}
4

4 に答える 4

1

=if ステートメントで使用しています。それが代入演算子です。==等値演算子である which を使用します。

「赤い線」が表示される理由は、「と等しいですか?」と尋ねるつもりだったときに、にcurrentStateなるべきだと言ったからです。State.GOcurrentStateState.GO


これは多くのエラーの 1 つにすぎません。別のエラーは次のとおりです。

public static void changeLight();

そこにセミコロンがあってはなりません。続くコードを中かっこで囲み、「これは私のメソッドのコードです」と言う必要があります。


それを修正するときは、次のものが必要です。

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


    }
}

ただし、このメソッドは静的でcurrentStateあり、静的変数ではないため、エラーになります。署名を次のように変更することで修正できます。

    public void changeLight()
于 2013-11-13T19:06:52.033 に答える
0

このような有限状態マシンとして列挙型を使用することをお勧めします

 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();
 }
于 2013-11-13T19:08:35.353 に答える
0

構文を確認してください。

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

編集: そしてもちろん == の代わりに =

于 2013-11-13T19:07:01.993 に答える