当用户输入的密码是:java时,程序将读取名字是scores.txt的文件。输入密码错误时显示提示信息,第3次输入密码错误时程序立刻退出。
java
boolean success = false;//输入是否正确
int count = 0;//输入次数
while(true){
//使用System.console().readPassword()可以在cmd控制台输入密码
}
if(success) {
// 读取文件内容显示
}
不懂怎么写出这段代码
帮忙采纳一下,亲,现写的。不懂可以问我
正确登录
输错3次
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String pwd="admin";
boolean success = false;//输入是否正确
//1.输入密码正确,提示登录成功 --- admin
//2.输入密码错误,提示密码输入错误,重新输入
//3.输入密码错误达到3次,提示输入密码错误达到3次,程序退出
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
int count = 0;
while(true) {
String password = sc.next();
if(password.equals(pwd)) {
System.out.println("登录成功!");
success=true;
break;//跳出循环
} else {
//3.判断密码错误达到3次
count++;
if(count == 3) {
System.out.println("密码错误达到3次,程序退出...");
return;//不是为了跳转出循环体,更常用的功能是结束一个方法
}
//2.密码错误,重新输入
System.out.println("密码输入错误,重新输入:");
continue;//进行下一次循环
}
}
if(success) {
// 读取文件内容显示
java.io.File file = new java.io.File("G:\\scores.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
pwd = input.next();
}
input.close();
System.out.println("文件密码="+pwd);
}
}
}
已调试通过,代码如下:
public static void main(String[] args) throws Exception {
boolean success = false;//输入是否正确
int count = 0;//输入次数
while(true){
if(3 == count) {
System.out.println("密码已错误3次,程序退出!");
System.exit(0);
}
System.out.print("请输入密码: ");
//使用System.console().readPassword()可以在cmd控制台输入密码
Console console = System.console();
char[] password = null;
if(null == console) {
Scanner scanner = new Scanner(System.in);
password = scanner.nextLine().toCharArray();
} else {
password = System.console().readPassword();
}
if(password==null) {
continue;
}
if(password.length!=4) {
++count;
continue;
}
char[] correctPass = "java".toCharArray();
for(int i=0; i<correctPass.length; ++i) {
if(correctPass[i] != password[i]) {
++count;
break;
}
if(i==correctPass.length-1) {
success = true;
}
}
if(success) {
break;
}
}
if(success) {
// 读取文件内容显示
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Administrator\\Desktop\\scores.txt")));
String content = null;
while((content=br.readLine())!=null) {
System.out.println(content);
}
}
}
运行结果如下
密码错误的情况
请输入密码: 123
请输入密码: 123123
请输入密码: 123
密码已错误3次,程序退出!
密码正确的情况
请输入密码: java
hello,wolrd!
我的文件内容是: hello,wolrd!
文件放在桌面上: C:\Users\Administrator\Desktop\scores.txt
如果你的文件放的路径变了,修改下程序里面的文件读取的路径
如有帮助,请采纳,十分感谢!
给个例子,
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
boolean success = false;//输入是否正确
int count = 0;//输入次数
while(true){
char[] cs = System.console().readPassword();
String pwd = new String(cs);
System.out.println(pwd);
if("java".equals(pwd)){
success = true;
break;
}else{
System.out.println("密码错误,请重新输入");
}
count++;
if(count==3){
System.exit(0);
}
}
if(success){
BufferedReader br;
try{
br = new BufferedReader(new FileReader("bin/scores.txt"));
String content;
while((content=br.readLine())!=null){
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestFile {
public static void main(String[] args) {
boolean success = false;// 输入是否正确
int count = 0;// 输入次数
while (true) {
char[] pwdArray = System.console().readPassword("输入密码");
String pwd = new String(pwdArray);
if(pwd.equals("java")) {
success=true;
break;
}
count ++;
if(count==3)//密码三次错误 立即退出
{
System.out.println("密码错误!系统退出");
System.exit(0);
}
}
if (success) {
Scanner input = null;
try {
input = new Scanner(new File("score.txt"));
// 读取文件内容显示
while(input.hasNext())
System.out.println(input.nextLine());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
input.close();
}
}
}
}
文件目录同级 如图
学习一下Java的输入输出流
在B站上面或者菜鸟编程上面学一下就理解了