gpt4 book ai didi

java - 在 Java 中操作类的构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:45 25 4
gpt4 key购买 nike

我有一个关于密码分析的练习(弱密码与强密码),我必须创建 2 个构造函数。其中之一必须接收密码的长度,并且密码必须随机创建,我该怎么做

此外,我无法确定密码是弱密码还是强密码。如果密码至少有 3 个大写字母、2 个小写字母和 6 个数字,则该密码会很安全。

class Password {
private double length;
private String password;

Password()//default constructor with password length 8
{
password="defaultt";
length=8;
}

Password(double length)
{
this.length=length;
password="";//generate a random password, how?
}

boolean isStrong(String password)
{
int totalnumbers=0;
int upper=0;
int lower=0;

for(int i=0;i<password.length;i++)//marks an error in .length why?
{
if (password.charAt(i)>='0' && password.charAt(i)<='9')
totalnumbers+=1;

if (password.charAt(i) is lowercase)//what can I do here?
lower+=1;

if (password.charAt(i) is uppercase)
upper+=1;
}

if(totalnumbers>=6 && lower>=2 && upper>=3)
return true;
else
return false;

}

generatePassword(double length)
{
password;//generate the password of the object with this.length //how to do it?
}

String getPassword()
{
return password;
}

double getLength()
{
return length;
}

void setLength(double length)
{
this.length=length;
}

}


public class Exercises2 {

public static void main(String[] args) {
Password x=new Password();
Password array[];
Scanner rd=new Scanner(System.in);
int Size;

System.out.println("Give the size of the passwords array ");
Size=rd.nextInt();
array=new Password[Size];

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

array[i]=new Password();//creation of an object for each position of the array
}

for(int j=0;j<Size;j++)
{
System.out.println("Give the size for each password of the array ");
array[j]=[rd.nextInt]; //length for each password of the array
}

Password arrayBooleans[]=new Password[Size];
for (int k=0;k<Size;k++){
arrayBooleans[k]=x.isStrong(array[k]);

}

for(int i=0;i<Size;i++)
{
System.out.println("password "+i+ arrayBooleans[i]);
}

}
}

最佳答案

首先,您不应该使用 double 作为密码中的字符数。你怎么能有1.5个字符?只需使用 int

要检查字符是数字、小写还是大写,请使用 Character 类(不要依赖 ASCII 值):

for (int i = 0; i < password.length(); i++) {

Character c = Character.valueOf(password.charAt(i));
if (c.isDigit()) {
totalnumbers++;
}

if (c.isLowerCase()) {
lower++;
}

if (c.isUpperCase()) {
upper++;
}
}

要生成给定长度的随机字母数字字符串,请查看此 SO post 的已接受答案.有许多不同的方法可以做到这一点,但这是一个很好的起点。

关于java - 在 Java 中操作类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626325/

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