Java 如何正确使用这个布尔值?
我正在做一个IRC机器人。如果有人键入“!tell username:message”,我希望能够查看该用户是否在线。下面是我代码的第一部分,它查看频道中的用户并检查用户是否已在线:Java 如何正确使用这个布尔值?,java,Java,我正在做一个IRC机器人。如果有人键入“!tell username:message”,我希望能够查看该用户是否在线。下面是我代码的第一部分,它查看频道中的用户并检查用户是否已在线: if (messageIC.startsWith("!tell ")) { boolean userIsOnChannel; String messagey = message.substring(6); String[] messager = messagey.split(":");
if (messageIC.startsWith("!tell ")) {
boolean userIsOnChannel;
String messagey = message.substring(6);
String[] messager = messagey.split(":");
String username = messager[0];
User[] users = getUsers(channel);
for (final User user : getUsers(channel)) {
if (user.getNick().equalsIgnoreCase(username)) {
userIsOnChannel = true;
sendMessage(channel, username + " is online now!");
break;
}
else {
userIsOnChannel = false;
}
}
}
所以当我在NetBeans中使用这个时,它告诉我变量userIsOnChannel从未被使用过。我想我在if/else语句中使用了它。这会产生问题,因为如果布尔值返回false,我希望能够存储用户的消息,并沿“我将传递该消息”的行向通道发送内容。当我尝试使用布尔值时,我得到一个错误,它从未被初始化过。我做错了什么?代码块一完成,该变量就超出范围。您可以通过设置它来“使用”它,但从不将其用作布尔值(您从不检查值) 你可能需要改变
if (messageIC.startsWith("!tell ")) {
boolean userIsOnChannel;
到
因此,userIsOnChannel在该代码块之后可用
您会得到“可能尚未初始化”,因为它不会通过写入来获得值
boolean userIsOnChannel;
您需要为其指定一个值(在本例中可能为false)如果默认布尔值为“false”,则将其初始化为false并删除else块
if (messageIC.startsWith("!tell ")) {
boolean userIsOnChannel = false;
String messagey = message.substring(6);
String[] messager = messagey.split(":");
String username = messager[0];
User[] users = getUsers(channel);
for (final User user : getUsers(channel)) {
if (user.getNick().equalsIgnoreCase(username)) {
userIsOnChannel = true;
sendMessage(channel, username + " is online now!");
break;
}
}
}
您需要一些东西来使用或返回字段 如果为字段添加getter方法,警告应该消失
getUserIsOnChannel(){
return userIsOnChannel;
}
在这里,我将尝试通过评论用简单的语言来解释它
boolean userIsOnChannel;
// at this place userIsChannel is undefinded and definitely you will
// try to get its value.
for(....) {
if(condition) {
userIsOnChannel = ...
} else {
userIsOnChannel = ...
}
// Here you will not get any error if you use userIsOnChannel
// because it is obvious that if if statement is not satisfied
// then else will be and userIsOnChannel will definitely be assigned.
}
// But here you will again get error because it compiler again here cannot
// confirm that the userIsOnChannel has a value (may be for statement not run even 1 time
因此,在使用变量之前,您必须初始化变量,编译器可以确认它的值将被分配。它已设置,但从未读取。@EJP当我尝试使用它时,我得到一个错误,它可能尚未初始化。您只是在初始化,而不是使用它。在第2行初始化它。想象一下,当cha上没有用户时的情况nnel。在这种情况下,您的代码在哪里为
userIsOnChannel
提供值?此外,for循环可能不会迭代,并且变量永远不会初始化。
boolean userIsOnChannel;
// at this place userIsChannel is undefinded and definitely you will
// try to get its value.
for(....) {
if(condition) {
userIsOnChannel = ...
} else {
userIsOnChannel = ...
}
// Here you will not get any error if you use userIsOnChannel
// because it is obvious that if if statement is not satisfied
// then else will be and userIsOnChannel will definitely be assigned.
}
// But here you will again get error because it compiler again here cannot
// confirm that the userIsOnChannel has a value (may be for statement not run even 1 time