我正在製作一個IRC bot。如果有人輸入「!tell username:message」,我希望能夠查看用戶是否在線。下面是我的代碼,它看起來穿過通道,並檢查用戶的第一部分,如果用戶已經在網上:當我有這個在NetBeans如何正確使用這個布爾值?
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;
}
}
}
所以,它告訴我,該變量userIsOnChannel是從未使用過。我以爲我在if/else語句中使用了它。這會產生問題,因爲我希望能夠存儲用戶的消息,並且如果布爾值返回false,則可以沿着「我將該消息傳遞給」通道的方式發送一些消息給通道。當我嘗試使用布爾值時,我得到一個從未初始化的錯誤。我做錯了什麼?
它已設置,但從未閱讀。 – EJP
@EJP當我嘗試使用它時,我收到一個錯誤,它可能沒有被初始化。 – quibblify
你只是初始化不使用它。在第2行初始化它。 – Jack