求助!密码流的java程序。

Se4u 2012-12-04 09:44:17
程序如下:

import java.io.*;
public class Password{
public static void main( String args[ ]) {
boolean success = false;
int count = 0;
Console cons;
// char[] passwd;
cons = System.console();
while(true) {
System.out.print("输入密码:");
char[]passwd = cons.readPassword();
count++;
String password=new String (passwd);
if (password.equals("tiger123")) {
success= true;
System.out.println("您第" + count + "次输人的密码正确!");
break;
}

else {
System.out.println("您第" + count + "次输人的密码" + password + "不正确");
}
if(count == 3) {
System. out. println("您" + count + "次输人的密码都不正确");
System.exit(0);
}
}
if(success) {
File file = new File( "score, txt");
try {
FileReader inOne = new FileReader(file);
BufferedReader inTwo= new BufferedReader(inOne);
String s= null;
while( (s = inTwo.readLine())!= null) {
System.out.println(s);
}
inOne.close();
inTwo.close();
}
catch (IOException exp) {}
}
}
}
程序没有报错,可是会出现下面的问题,请教一下,到底是什么原因(下面的异常我懂),就是不知道怎么改。
...全文
256 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Se4u 2012-12-09
  • 打赏
  • 举报
回复
对啊,我用命令行运行了一下,可以的。不过我想用eclipse运行,可以吗?
wikidemoon 2012-12-08
  • 打赏
  • 举报
回复
能不能在eclipse中使这个程序也运行正确啊
  • 打赏
  • 举报
回复
你把他存为test.java 在cmd里 javac test.java 然后java test 可以运行的。 eclipse你建的项目造成执行这个cons.readPassword()有问题。
Se4u 2012-12-08
  • 打赏
  • 举报
回复
那具体要怎么做才能运行正常呢?
地球原住民 2012-12-04
  • 打赏
  • 举报
回复
这个是运行时错误。System.console() 函数只有在JVM 关联到一个控制台的时候才会返回非空值,在eclipse 中执行返回null。如果在控制台执行 java Password,运行是正常的。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧