源程序如下:
import java.io.*;
public class Echo {
private static String Hello;public static void main(String[] args) {
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
String s=Hello;
try {
while((s = in.readLine()).length() != 0)
System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
}
}
程序中String s=Hello;根据这两条语句:while((s = in.readLine()).length() != 0) System.out.println(s);
程序应该输出Hello才对,怎么结果是什么都没有呢?
import java.io.*;
public class Echo {
private static String Hello; public static void main(String[] args) {
String inputstr;
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
String s="Hello";
try {
if((inputstr = in.readLine()).length() != 0)
System.out.println(inputstr);
else System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
}
}
希望能够帮到lz
import java.io.*;
public class Echo {
private static String Hello; public static void main(String[] args) {
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
String s=Hello;
try {
while((s = in.readLine()).length() != 0) {}
System.out.println(s);
Thread.sleep(2000); //放大效果,可以看到实际上输出 了空 , 然后程序结束
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException w){} }
}