gpt4 book ai didi

java - 大小写切换java编译错误

转载 作者:行者123 更新时间:2023-11-29 05:24:10 25 4
gpt4 key购买 nike

我写了下面的代码但是得到了下面的编译错误:

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/

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