英文:
Trying to store randomly generated password as a string so then i can save it to a file (Java)
问题
以下是翻译好的内容:
我正在尝试创建一个随机密码生成器,我已经成功让系统根据用户输入的字符长度生成随机字符串。接下来,我想要的是询问用户是否想要保存文件。在文件中,我想要包含网站/应用的名称和密码。
我的问题是,我无法将密码的随机字符串存储为变量,有谁知道如何解决这个问题?
代码如下所示:
System.out.println("请输入您需要的密码长度:");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
for (int i = 0; i < passwordLength; i++) {
System.out.print(alphabet.charAt(r.nextInt(alphabet.length())));
}
英文:
So what I am trying to do is create a random password generator, I have managed to make the system produce a random string of characters based on the char length that the user inputs. What I then want to do is ask the user if they want to save the file. On the file I want to include the name of the website/application and the password.
My problem is I cannot get the random string for the password to be stored as a variable, does anyone know how to fix this?
Code can be seen below:
System.out.println("Please enter the length of password that you require: ");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
for (int i = 0; i < passwordLength; i++) {
System.out.print(alphabet.charAt(r.nextInt(alphabet.length())));
}
答案1
得分: 1
我相信您的意思是希望将密码收集到一个字符串变量中,而不是打印每个字符。以下是一个完整工作示例:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您需要的密码长度:");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < passwordLength; i++)
sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
String password = sb.toString();
System.out.print(password);
}
结果:
请输入您需要的密码长度:
12
j$miDCadnJMd
英文:
I believe you mean that you want to collect the password into a String variable instead of printing each character. Here's how to do that in a completely working example:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the length of password that you require: ");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < passwordLength; i++)
sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
String password = sb.toString();
System.out.print(password);
}
Result:
Please enter the length of password that you require:
12
j$miDCadnJMd
答案2
得分: 0
如果您不想使用字符串生成器,可以这样做:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入所需的密码长度:");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
String password = "";
for (int i = 0; i < passwordLength; i++)
password += (alphabet.charAt(r.nextInt(alphabet.length()))) + "";
System.out.print(password);
}
请注意,我只翻译了代码部分,不包括注释和其他内容。
英文:
If you dont want to use a String Builder, you could do this
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the length of password that you require: ");
int passwordLength = sc.nextInt();
sc.nextLine();
Random r = new Random();
String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
String password = "";
for (int i = 0; i < passwordLength; i++)
password += (alphabet.charAt(r.nextInt(alphabet.length()))) + "";
System.out.print(password);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论