我想创建一个生成随机密码的程序,并要求用户猜测密码
我正在创建一个程序,该程序生成随机密码,并要求用户猜测生成的密码。 我现在面临的问题是如何将生成的密码与输入密码进行比较 我的程序比较了两个密码的长度,但是我不知道比较两个密码以查看它们是否相同的方法 该程序必须比较两个密码,并且经过三次尝试,该程序必须输出生成的密码 下面的代码是我的程序
final static Scanner in = new Scanner(System.in);
public static String method(int len){
String ASCII = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z";
int count =0;
int rand;
System.out.println("Guess the generated password");
String key = in.nextLine();
for(int i=0;i<key.length();i++){
count++;
}
SecureRandom sr = new SecureRandom();
StringBuilder sb = new StringBuilder();
for(int i =0;i<len;i++){
rand= sr.nextInt(ASCII.length());
sb.append(ASCII.charAt(rand));
}
for(int i=0;i<3;i++){
if(len!=count){
System.out.println("The length of the random generated password is "+ len+ " and the length of the password inserted is " + count);
System.out.println("The length of the generated password and the length of the inserted password varies. Please try again...");
System.out.println("Guess the generated password again");
key = in.nextLine();
}
}
return sb.toString();
}
public static void main(String[] args) {
Random r = new Random();
int len;
len = r.nextInt(1, 8);
System.out.println("length :" + len);
System.out.println(method( len));
}
}
I am creating a program that generates a random password and asks the user to guess the generated password.
The problem I'm facing now is how to compare the generated password to the inputted password
My program compares the length of both passwords, but I don't know any possible way to compare both passwords to see if they're the same or not
The program has to compare both passwords and after three attempts, the program has to output the generated password
The code below is my program
final static Scanner in = new Scanner(System.in);
public static String method(int len){
String ASCII = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z";
int count =0;
int rand;
System.out.println("Guess the generated password");
String key = in.nextLine();
for(int i=0;i<key.length();i++){
count++;
}
SecureRandom sr = new SecureRandom();
StringBuilder sb = new StringBuilder();
for(int i =0;i<len;i++){
rand= sr.nextInt(ASCII.length());
sb.append(ASCII.charAt(rand));
}
for(int i=0;i<3;i++){
if(len!=count){
System.out.println("The length of the random generated password is "+ len+ " and the length of the password inserted is " + count);
System.out.println("The length of the generated password and the length of the inserted password varies. Please try again...");
System.out.println("Guess the generated password again");
key = in.nextLine();
}
}
return sb.toString();
}
public static void main(String[] args) {
Random r = new Random();
int len;
len = r.nextInt(1, 8);
System.out.println("length :" + len);
System.out.println(method( len));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于本文最初放置,因此您已经在编码中取得了一些进步。 基于下面显示的最新代码(从您删除的帖子中):
在特定范围内生成一个随机数(例如1包含7包)时,请这样做:
不要添加一堆
ascii
字符串变量中的空格。考虑根本不具备任何空格,或者如果需要的话,只需在上下字母之间提供一个:我这么说,因为可能有可能您的随机密码生成 可以创建一个密码是所有实际上不是密码的空格。
另外,我知道您的编译器不会抱怨,但是您的代码读者将...考虑使用适当的变量命名约定。即使
ascii
除非它将是类字段变量,否则不是。目前,它被认为是 local 变量。实际上,您在这个较旧的帖子中正确地做到了这一点。如果尚未生成自动生成的密码,则无法猜测自动生成的密码。自动生成密码以猜测的代码是第一个要求您提供其本质的提示。
移动在上面生成密码的代码询问该密码的第一个提示的代码,例如:
此 for 在第一个猜测提示下循环:
不是必需的。它所做的就是计算第一个密码猜测中包含的字符数。具有讽刺意味的是,您已经通过仅使用
key.length()
方法来知道这一点。无论如何,您都不会将Count
变量用于任何内容,因此您不妨摆脱所有这些:实际上,您只需要一个提示,并且总是很高兴验证用户的输入。这是这样做的另一种方法:
You have made some strides in your coding since this post was originally placed. Based on your newest code shown below (from the post you deleted):
When generating a random number within a specific range like 1 inclusive to 7 inclusive then do it this way:
Don't add a bunch of whitespaces in your
ASCII
String variable. Consider not having whitespaces at all or if you have to, just provide one between the Upper and lower letters, for example:I say this because potentially, there is a good chance your random password generation could create a password that is all whitespaces which really isn't a password at all.
Also, I know your compiler isn't complaining but readers of your code will....consider using proper naming conventions for your variables, etc.
ASCII
should beascii
or evenaSCII
unless it is going to be a class Field variable which it is not. It is currently considered a method local variable. You actually did this correctly in this older post.You can not guess an auto-generated password if that auto-generated password has not been generated yet. The code which auto-generates the password to guess is currently after the first prompt which asks you to supply what it is.
Move the code that generates the password above the code that asks the first prompt of what that password might be, for example:
This
for
loop under the first Guess prompt:is not required. All it does is count the number of characters contained within the first password guess. Ironically, you already know this by just using the
key.length()
method. You don't use thecount
variable for anything anyways so you may as well just get rid of all that:In reality, you only need one prompt and it's always nice to validate the User's input. Here is an alternative way to do this: