console在eclipse 下报错?
import java.util.*;
import java.io.*;
import java.math.*;
public class Welcome
{
public static void main(String[] args)
{
Console cons = System.console();
username = cons.readLine("%s","Username:");
char[] password = cons.readPassword("%s","Password:");
}
}
这段代码为什么在eclipse下编会报错,而在命令行模式下没有问题?
eclipse 报错如下:
Exception in thread "main"
java.lang.Error: Unresolved compilation problem:
username cannot be resolved to a variable
at Welcome.main(Welcome.java:18)
------解决方案--------------------Console cons = System.console();
if(cons != null){
String username = cons.readLine("%s","Username:");
char[] password = cons.readPassword("%s","Password:");
}