2015-04-03 36 views
0
public class ClientServer { 

public static void main (String[] args){ 

    Object[] selectioValues = {"Server "," Client"}; 
    String initialSection = "Server"; 
    Object selection = JOptionPane.showInputDialog(null,"login As:","Client server", JOptionPane.QUESTION_MESSAGE,null , selectioValues , initialSection); 

    **if(selection.equals("Server"))**{ 
     server srv = new server(); 
     srv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     srv.startRunning(); 

    } 

    **else if (selection.equals("Client"))**{ 
     String IPServer = JOptionPane.showInputDialog("enter IP:"); 
     Client capsa; 
     capsa = new Client(IPServer); 
     capsa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     capsa.startRunning(); 
    } 
} 

} 

粗体代码不起作用。当我添加断点来检查编译器是否进入IF内部时,它不是。请解释为什么IF条件不运行。与字符串值的对象值比较不会发生

+0

编译器不会执行您的代码。 “选择”不能等于“服务器”。 – 2015-04-03 17:09:14

+0

我重新检查了我的代码.selection的值是“Server”。我没有找到尾部空格 – Snakeeyes 2015-04-04 05:53:34

回答

2

您可能的选择值是"Server "" Client",但您可以与"Server""Client"进行比较。由于前导/尾随空格,字符串不相等。