用java写一个交通信号灯枚举类TrafficLight并在Test类中输出每种灯颜色的RGB值
时间: 2024-05-14 08:14:48 浏览: 146
TrafficLight.java:
```
public enum TrafficLight {
RED(255, 0, 0),
YELLOW(255, 255, 0),
GREEN(0, 255, 0);
private int red;
private int green;
private int blue;
TrafficLight(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return red;
}
public int getGreen() {
return green;
}
public int getBlue() {
return blue;
}
}
```
Test.java:
```
public class Test {
public static void main(String[] args) {
for (TrafficLight light : TrafficLight.values()) {
System.out.println(light.name() + ": RGB(" + light.getRed() + ", " + light.getGreen() + ", " + light.getBlue() + ")");
}
}
}
```
输出结果:
```
RED: RGB(255, 0, 0)
YELLOW: RGB(255, 255, 0)
GREEN: RGB(0, 255, 0)
```
阅读全文