if (!(portField.getText().equals(""))) {
String p = portField.getText();
CharSequence numbers = "0123456789";
if (p.contains(numbers)) {
listener = new ServerSocket(Integer.parseInt(p));
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hi there, human.");
} finally {
socket.close();
}
}} else {
JOptionPane.showMessageDialog(null, "Only numbers are allowed.");
}
} else {
JOptionPane.showMessageDialog(null, "Please input a port.");
}
问题是:JOptionPane
即使我在portField
. 据CharSequence
我所知,我测试它只允许输入数字的方式是正确的,Java 会忽略整个块并跳转到else
子句。
为什么会这样?我不应该使用else
和使用else if
吗?