gpt4 book ai didi

Java - 密码生成器

转载 作者:行者123 更新时间:2023-12-01 11:02:36 25 4
gpt4 key购买 nike

我正在将其作为一个项目来完成。其要点是使用您选择的字符类型和您选择的长度创建一个密码。但是当我使用选项 D 时,它不会返回任何内容,而在选项 B 和 C 上,无论我输入的长度如何,我总是会得到随机数量的字符。如果您可以运行它,并为我指出解决此问题的正确方向,我将不胜感激。

import java.util.Scanner;

public class Password
{
public static void main(String [] args){
String password = "";
String temp = "";
int randLetter = 0;
int randNumber = 0;
int randPunct = 0;
int charSelection = 0;
int counter = 0;
String lowerCase = "abcdefghijklmnopqrstuvwxyz";
String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String number = "0123456789";
String punctuation = "!?";


Scanner in = new Scanner(System.in);

System.out.println("Please choose what characters you would like to use.");
System.out.println("[A] Lowercase Letters");
System.out.println("[B] Lowercase & Uppercase Letters");
System.out.println("[C] Lowercase, Uppercase, Numbers");
System.out.println("[D] Lowercase, Uppercase, Numbers, Punctuation");
System.out.print("Selection: ");
String selection = in.next();

System.out.println();
System.out.print("Select a length (1 - 14): ");
int length = in.nextInt();

if(selection.equalsIgnoreCase("A")){
for (int i = 0; i < length; i++){
randLetter = 1 + (int)(Math.random() * 26);
temp = lowerCase.substring(randLetter - 1 , randLetter);
password += temp;
}
}
else if(selection.equalsIgnoreCase("B")){
while (counter < length){
for (int i = 0; i < length; i++){

charSelection = 1 + (int)(Math.random() * 10);

if (charSelection < 5){

randLetter = 1 + (int)(Math.random() * 26);
temp = lowerCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
else if (charSelection > 5 && charSelection < 10){

randLetter = 1 + (int)(Math.random() * 26);
temp = upperCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
}
}
}
else if(selection.equalsIgnoreCase("C")){
while (counter < length){
for (int i = 0; i < length; i++){

charSelection = 1 + (int)(Math.random() * 17);
randNumber = 1 + (int)(Math.random() * 9);

if (charSelection < 5){

randLetter = 1 + (int)(Math.random() * 26);
temp = lowerCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
else if (charSelection > 5 && charSelection < 10){

randLetter = 1 + (int)(Math.random() * 26);
temp = upperCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
else if (charSelection > 15 && charSelection < 17){

randLetter = 1 + (int)(Math.random() * 26);
temp = number.substring(randNumber - 1 , randNumber);
counter++;
password += temp;
}
}
}
}
else if(selection.equalsIgnoreCase("D")){
while (counter < length){
for (int i = 1; i < 0; i++){

charSelection = 1 + (int)(Math.random() * 20);
randNumber = 1 + (int)(Math.random() * 9);
randPunct = 1 + (int)(Math.random() * 2);
if (charSelection < 5){

randLetter = 1 + (int)(Math.random() * 26);
temp = lowerCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
else if (charSelection > 5 && charSelection < 10){

randLetter = 1 + (int)(Math.random() * 26);
temp = upperCase.substring(randLetter - 1 , randLetter);
counter++;
password += temp;
}
else if (charSelection > 15 && charSelection < 17){

randLetter = 1 + (int)(Math.random() * 26);
temp = number.substring(randNumber - 1 , randNumber);
counter++;
password += temp;
}
else if (charSelection > 17){

randLetter = 1 + (int)(Math.random() * 26);
temp = punctuation.substring(randPunct - 1 , randPunct);
counter++;
password += temp;
}
}
}
}
System.out.println(password);
}
}

最佳答案

for (int i = 1; i < 0; i++){

这个条件永远不会成立,因此循环体永远不会执行。

目前尚不清楚您在这里的真正意图,因此很难建议正确的条件 - 您的意思是 length 而不是 0,就像您在其他循环中所做的那样?

关于Java - 密码生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com