简体   繁体   English

如何正确使用此布尔值?

[英]How to properly use this boolean?

I'm making an IRC bot. 我正在制作一个IRC机器人。 If someone types "!tell username: message" I want to be able to see if the user is online or not. 如果有人键入“!tell username:message”,我希望能够查看用户是否在线。 Here's the first part of my code, which looks through the users in the channel and checks if the user is already online: 这是我的代码的第一部分,它会检查通道中的用户并检查用户是否已经在线:

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;
        }
    }

}

So when I have this in NetBeans, it's telling me that the variable userIsOnChannel is never used. 因此,当我在NetBeans中使用它时,它告诉我从未使用变量userIsOnChannel。 I thought I used it in the if/else statements. 我以为我在if / else语句中使用了它。 This creates problems because I want to be able to store the user's message and send something along the lines of "I'll pass that along" to the channel if the boolean returns false. 这会产生问题,因为如果布尔值返回false,我希望能够存储用户的消息并沿“我将传递”的内容发送到通道。 When I try to use the boolean, I get an error that it has never been initialized. 当我尝试使用布尔值时,出现一个错误,提示它从未初始化。 What did I do wrong? 我做错了什么?

That variable goes out of scope as soon as your code block finishes. 代码块完成后,该变量将超出范围。 You "use" it by setting it but you never use it as a boolean (you never check the value. 您可以通过设置“使用”它,但是您永远不会其用作布尔值(从不检查该值。

You probably need to change 您可能需要更改

if (messageIC.startsWith("!tell ")) {
boolean userIsOnChannel;

to

boolean userIsOnChannel = false;
if (messageIC.startsWith("!tell ")) {

so that userIsOnChannel is available after that code block. 因此在该代码块之后, userIsOnChannel可用。

You get the "may not have been initialized" because it does not get a value just by writing 您会收到“可能尚未初始化”的信息,因为它不能仅通过编写来获取值

boolean userIsOnChannel;

you need to assign it a value (probably false in this case).. 您需要为其分配一个值(在这种情况下,可能为false)。

If the default boolean value is "false" then initialize it to false and remove the else block. 如果默认布尔值是“ 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;
        }

    }

}

You need to have something use or return the field. 您需要使用或返回该字段。

If you add a getter method for the field the warning should go away. 如果为该字段添加吸气剂方法,则警告将消失。

getUserIsOnChannel(){
return userIsOnChannel;
}

Here I will try to explain it in simple words through comments. 在这里,我将尝试通过注释用简单的词来解释它。

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

So you will have to initialize your variable before you use it and compiler could confirm that its value will be assigned. 因此,在使用变量之前必须先对其进行初始化,编译器可以确认将为其赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM