Java 有人能解释一下这个算法吗?
在我正在阅读的一本书中,我在许多例子中看到了这一点,但作者没有解释算法是什么?字符串主机=args.length>0?args[0]:本地主机Java 有人能解释一下这个算法吗?,java,algorithm,sockets,Java,Algorithm,Sockets,在我正在阅读的一本书中,我在许多例子中看到了这一点,但作者没有解释算法是什么?字符串主机=args.length>0?args[0]:本地主机 import java.net.*; import java.io.*; public class Test { public static void main(String[] args) { String host = args.length > 0 ? args[0] : "localhost";//This is the part
import java.net.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
String host = args.length > 0 ? args[0] : "localhost";//This is the part that I don't get
for (int i = 1; i < 1024; i++) {
try {
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "
+ host);
s.close();
} catch (UnknownHostException ex) {
System.err.println(ex);
break;
} catch (IOException ex) {
// must not be a server on this port
}
}
}
}
您注释的行将主机字符串设置为第一个命令行参数,如果不存在任何命令行参数,则将主机字符串设置为localhost 它使用三元运算符,基本上展开如下:
String host;
if (args.length > 0){
host = args[0];
}else{
host = "localhost";
}
谷歌Java甚至不知道他们被称为“谢谢”的问题。如果你不知道它叫什么,就很难查到。等等。在谷歌搜索之前,你能猜出它的意思吗?如果你猜不到,你能试着找出答案吗?使用参数运行此程序时,主机的值是多少?那没有呢?大部分情况下我都没解开钥匙?以及本地主机部分。我不知道它们是什么,现在我知道了,谢谢你的帮助