我需要做IRC客户端,我遇到登录问题。我试图循环通过服务器输入找到按钮004或433,其中指出成功登录或登录使用。这是我的代码:`
import ecs100.*;
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ChatClient {
private String server = "irc.ecs.vuw.ac.nz";
private static final int IRC_PORT = 6667;
private static String nick;
private static String name;
private Socket socket;
private Scanner serverIn;
private PrintStream serverOut;
private String line;
private String channel;
// Constructor
public ChatClient (){
this.setupGUI();
}
public void setupGUI(){
UI.addButton("Connect", this::connect);
}
public void connect(){
try {
socket = new Socket(server,IRC_PORT);
serverIn = new Scanner(socket.getInputStream());
serverOut = new PrintStream(socket.getOutputStream());
UI.println("login");
this.login();
UI.println("listening");
this.listenToServer();
}catch(IOException e){UI.println("IO failure "+ e);}
}
private boolean login(){
String username = UI.askToken("Enter your usercode: ");
String realname = UI.askString("Enter your real name: ");
this.nick = username;
this.name = realname;
send("NICK " + username);
send("USER " + username + " 0 unused :" + realname);
UI.println("listening to server");
while(this.serverIn.hasNext()){
this.line = serverIn.nextLine();
UI.println("SERVER" + line);
if (line.indexOf("004") >= 0){
UI.println("true");
return true;
} else if(line.indexOf("433") >= 0){
UI.println("failed to connect, username already in use");
return false;
} else {
UI.println("else");
return false;
}
}
}
private void send(String msg){
serverOut.print(msg + "\r\n");
serverOut.flush();
}
private void listenToServer() {
while(this.serverIn.hasNext()){
String line = serverIn.nextLine();
UI.println("SERVER" + line);
if(line.equals("SQUIT")){
this.closeConnection();
}
if(line.equals("PING")){
String pingMsg = line.split(" ",2)[1];
this.send("PONG " + pingMsg);
}
}
}
public void closeConnection(){
try {
this.socket.close();
this.serverIn.close();
this.serverOut.close();
UI.println("Closed");
} catch(IOException e){UI.println("IO failure "+ e);}
}
public void joinChannel(){
String channel = UI.askString("Enter channel name: ");
send("JOIN " + channel);
}
public void leaveChannel(){
send("PART " + channel);
}
public static void main(String[] args) {
new ChatClient();
}
}
问题是它在登录方法中不断丢失返回语句。任何人都可以看看我的代码,看看这里可能有什么问题?
编辑: 好的,所以当我将if语句更改为两个if并且在循环之外放置return false时,程序可以正常工作。有人可以解释为什么while循环中的if / else语句不起作用吗?
private boolean login(){
String username = UI.askToken("Enter your usercode: ");
String realname = UI.askString("Enter your real name: ");
this.nick = username;
this.name = realname;
send("NICK " + username);
send("USER " + username + " 0 unused :" + realname);
UI.println("listening to server");
while(this.serverIn.hasNext()){
this.line = serverIn.nextLine();
UI.println("SERVER" + line);
if (line.indexOf("004") >= 0){
UI.println("true");
return true;
}
if(line.indexOf("433") >= 0){
UI.println("failed to connect, username already in use");
return false;
}
}
UI.println("else");
return false;
}
enter code here
答案 0 :(得分:0)
因为有一个代码路径会导致没有return
被调用,并且编译器不能允许这种方法用于非void方法。
如果this.serverIn.hasNext()
返回false,如果你最后没有返回语句,该方法会返回什么?毕竟它需要返回boolean
。
可以编写程序逻辑,以便从不采用特定的代码路径,但理论上它是可能的,这就是编译器抱怨的原因。