gpt4 book ai didi

java - 如何将 Enum 的值与 Enum 方法中 Enum 的所有可能值进行比较并避免丢失 return 语句?

转载 作者:行者123 更新时间:2023-12-01 17:46:31 43 4
gpt4 key购买 nike

我刚刚学习 Java 中的枚举。当我运行下面的代码时,我收到一个错误,我也在下面重现该错误。基本上,我的问题是:当我在枚举中定义一个方法,并且在该方法中我想检查枚举的值以便我可以根据该值执行某些操作时,如何执行此检查?下面我有一个具有三个可能值的枚举,在方法 getNext 中,我有三个 if 语句将此枚举的值与三个可能值中的每一个进行比较。但我仍然收到错误,说有一条没有返回的路径。

package enumerations;

enum TrafficLightColor2 {
RED(12), GREEN(10), YELLOW(2);

private int waitTime;

TrafficLightColor2(int waitTime) {
this.waitTime = waitTime;
}

int getWaitTime() {
return waitTime;
}

TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}
}

// A computerized traffic light.
class TrafficLightSimulator2 implements Runnable {
private Thread thrd; // holds the thread that runs the simulation
private TrafficLightColor2 tlc; // holds the traffic light color
boolean stop = false; // set to true to stop the simulation
boolean changed = false; // true when the light has changed

TrafficLightSimulator2(TrafficLightColor2 init) {
tlc = init;
thrd = new Thread(this);
thrd.start();
}

TrafficLightSimulator2() {
tlc = TrafficLightColor2.RED;
thrd = new Thread(this);
thrd.start();
}

// Start up the light.
public void run() {
while (!stop) {
try {
Thread.sleep(tlc.getWaitTime());
} catch (InterruptedException exc) {
System.out.println(exc);
}
changeColor();
}
}

// Change color.
synchronized void changeColor() {
tlc = tlc.getNext();
changed = true;
notify(); // signal that the light has changed
}

// Wait until a light change occurs.
synchronized void waitForChange() {
try {
while (!changed)
wait(); // wait for light to change
changed = false;
} catch (InterruptedException exc) {
System.out.println(exc);
}
}

// Return current color.
synchronized TrafficLightColor2 getColor() {
return tlc;
}

// Stop the traffic light.
synchronized void cancel() {
stop = true;
}
}


class TrafficLightDemo2 {
public static void main(String args[]) {
TrafficLightSimulator tl =
new TrafficLightSimulator(TrafficLightColor.GREEN);
for (int i = 0; i < 9; i++) {
System.out.println(tl.getColor());
tl.waitForChange();
}

tl.cancel();
}
}

我收到错误

$ javac enumerations/TrafficLightDemo2.java
enumerations/TrafficLightDemo2.java:26: error: missing return statement
}
^
1 error

最佳答案

TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}

如果所有 3 个 if 都为 false,则此方法不会返回值。

在处添加 return 或更好地抛出错误,例如

抛出新的 IllegalArgumentException("不支持的枚举")

关于java - 如何将 Enum 的值与 Enum 方法中 Enum 的所有可能值进行比较并避免丢失 return 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54538933/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com