编写代码: 程序在读取文件时,要求用户输入的密码是tiger123。如果输入了正确的密码,程序将读取名字是word.txt的文件。程序允许用户两次输入的密码不正确,一旦超过两次,程序将立刻退出。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("输入密码:"); 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) { //从文件中读取数据 } } }
时间: 2023-06-16 22:02:57 浏览: 56
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("输入密码:");
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) {
try {
BufferedReader br = new BufferedReader(new FileReader("word.txt"));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch(IOException e) {
System.out.println("文件读取错误");
}
}
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)