package StreetKing;
import java.awt.Color;
public class TrffcLgt {
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* This class represents a simple implementation of a stoplight.
* The client can determine the current state of the stoplight by
* calling stoplight.getState() and change it to the next color
* in the sequence (GREEN -> YELLOW -> RED -> GREEN) by calling
* stoplight.advance().
*/
public class Stoplight {
/** Constant indicating the color GREEN */
public static final Color GREEN = Color.GREEN;
/** Constant indicating the color YELLOW */
public static final Color YELLOW = Color.YELLOW;
/** Constant indicating the color RED */
public static final Color RED = Color.RED;
/**
* Creates a new Stoplight object, which is initially GREEN.
*/
public Stoplight() {
state = 0;
}
/**
* Returns the current state of the stoplight.
* @return The state of the stoplight (GREEN, YELLOW, or RED)
*/
public Color getState() {
switch (state) {
case 0: return GREEN;
case 1: return YELLOW;
case 2: return RED;
default: return null; /* Can't occur but required by Java */
}
}
/**
* Advances the stoplight to the next state.
*/
public void advance() {
state = (state + 1) % 3;
}
/* Private instance variable */
private int state;
}
}
}
获取错误消息为“非法修饰符为本地类Stoplight;只允许抽象或最终”。这应该是什么解决方案?我对java很新。获取下面列出的错误消息
把'Stoplight'并在'main'方法外声明 - 然后,你可以通过'Stoplight sl = new Stoplight );'从'main'方法内部 – ochi 2014-10-19 07:12:52
完成但仍然出错“它给出了一个错误,”公共类型的Stoplight必须在它自己的文件中定义“ – 2014-10-19 07:56:25