你没有张贴任何代码,所以我会假设你有一个password
变量定义和你的程序看起来是这样的:
Scanner userInput = new Scanner(System.in);
String password = "Default01";
System.out.print("Enter new password: ");
password = userInput.next();
每次运行程序时,它会创建在RAM中,一个全新的password
变量实例。当程序关闭时,RAM中的任何内容都将被销毁。您需要某种持久性存储,将这些信息写入变量。一个文本文件是一个简单的方法来开始。添加这将使您的程序看起来像:
Scanner userInput = new Scanner(System.in);
File passwordFile = new File("passwordfile.txt");
//this is where the password is stored.
Scanner passwordScanner = new Scanner(passwordFile);
//this is how you read the file.
String password = passwordScanner.next();
//password has been read.
...然后提示输入新密码。
System.out.print("Enter new password: ");
password = userInput.next(); //prompt for new password
...然后将该新密码写入文件进行永久存储。
PrintWriter passwordWriter = new PrintWriter("passwordfile.txt");
// overwrites the current passwordfile.txt, so you now have an empty file
passwordWriter.print(password);
//writes the password to passwordfile.txt so it can be used next time.
希望这会有所帮助!