我写了下面的代码但是得到了下面的编译错误:
The local variable dirArrow may not have been initialized. Note that a problem regarding missing 'default:' on 'switch' has been suppressed, which is perhaps related to this problem
//return the ID of the robot and arrow of his facing direction
public String toString(){
char dirArrow;
switch (direction) {
case UP: dirArrow= '^';
case RIGHT: dirArrow= '>';
case DOWN: dirArrow= 'V';
case LEFT: dirArrow= '<';
break;
}
return (Integer.toString(RoboID) + dirArrow);
}
最佳答案
您需要像这样初始化您的 dirArrow
变量:
char dirArrow = ' ';
switch (direction) {
阅读为什么本地 variable should be initialized .
注意:您还需要在每个 case block 的末尾添加一个 break
语句,例如:
case UP: {
dirArrow= '^';
break;
}
关于java - 大小写切换java编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363278/