import java.io.*;
public class ConsoleDemo {
public static void main(String[] args) {
String str;
Console con;
con = System.console();
if (con == null)
return;
str = con.readLine("Enter a string : ");
con.printf("Here is your string %s", str);
}
}
我从书上复制了这段代码,上面说我会在屏幕上提示输入字符串,但我的IDE只是给出执行已终止的消息,而没有给我提示。
最佳答案
Eclipse 和 Netbeans 都不支持使用控制台。 Console.istty() 方法将返回 false,并且您将无法使用控制台。
您可以将代码更改为以下内容并获得相同的结果,并能够从 IDE 中运行它。
import java.io.*;
public class ConsoleDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a String and press enter");
System.out.println("You entered the String: " + scan.nextLine()
}
}
关于java - 为什么控制台不提示输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10985462/