当前位置: 代码迷 >> J2SE >> console在eclipse 停报错
  详细解决方案

console在eclipse 停报错

热度:80   发布时间:2016-04-23 22:23:20.0
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:");
}
  相关解决方案