Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中操作类的构造函数_Java_Arrays_Object_Constructor - Fatal编程技术网

在Java中操作类的构造函数

在Java中操作类的构造函数,java,arrays,object,constructor,Java,Arrays,Object,Constructor,我有一个关于密码分析的练习(弱与强),我必须创建两个构造函数。其中一个必须接收密码的长度,并且密码必须随机创建,我如何才能做到这一点? 此外,我很难确定密码是弱还是强。如果密码至少有3个大写字母、2个小写字母和6个数字,那么它将是强大的 class Password { private double length; private String password; Password()//default constructor with password length

我有一个关于密码分析的练习(弱与强),我必须创建两个构造函数。其中一个必须接收密码的长度,并且密码必须随机创建,我如何才能做到这一点

此外,我很难确定密码是弱还是强。如果密码至少有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]);
        }   

    }
}
类密码{
私人双倍长度;
私有字符串密码;
Password()//密码长度为8的默认构造函数
{ 
password=“defaultt”;
长度=8;
}
密码(双倍长度)
{
这个。长度=长度;
password=”“;//如何生成随机密码?
}
布尔isStrong(字符串密码)
{ 
整数总数=0;
整数上限=0;
整数下限=0;
对于(int i=0;i=0’&&password.charAt(i)=6&&lower>=2&&lower>=3)
返回true;
其他的
返回false;
}
generatePassword(双倍长度)
{
password;//生成具有此长度的对象的密码//如何操作?
}
字符串getPassword()
{
返回密码;
}
双getLength()
{
返回长度;
}
空隙设置长度(双倍长度)
{
这个。长度=长度;
}
}
公开课练习2{
公共静态void main(字符串[]args){
密码x=新密码();
密码数组[];
扫描仪rd=新扫描仪(System.in);
整数大小;
System.out.println(“给出密码数组的大小”);
Size=rd.nextInt();
数组=新密码[大小];

对于(int i=0;i您在这里有几个不同的问题,因此我将尽力回答所有问题:

随机密码:

谈到随机密码生成,所以要了解更多详细信息,请查看发布的内容,但本质上您需要使用Java内置的一些随机生成,并从中创建一个字符串。有许多不同的方法可以实现,这在该问题中有详细说明,因此我将让您决定如何实现

循环的
中出现错误:

调用
password.length
尝试访问password对象中名为
length
的变量。在Java中,您不能这样做,必须使用以下方法:

password.length()
大小写检测

显示了一种根据字符值检测字符是大写还是小写的好方法。代码段:

if(input>='a'&&input<='z')
    //lowercase

if(input>='A'&&input<='Z')
    //uppercase

如果(input>='a'&&input='a'&&input首先,您不应该对密码中的字符数使用
double
。您怎么可能有1.5个字符?只需使用
int

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

for(int i=0;i

要生成给定长度的随机字母数字字符串,请查看公认的答案。有许多不同的方法,但这是一个很好的起点。

尝试使用简单的逻辑

Use Password()
{
// Simply check length
if(length == 8)
{
// Call a method which would check that password is strong or weak. for e.g, 
}
}
//您可以使用类java.util.Random with方法生成密码 例如

 char c = (char)(rnd.nextInt(128-32))+32 
 20x to get Bytes, which you interpret as ASCII. // If you're fine with ASCII.
// Call a method which would check that password is strong or weak.
使用给定链接随机生成密码:


我认为必须有比比较ASCII值更好的方法来检查字符是否为大写/小写我看到了随机生成密码的代码(在第一个链接中),但我不知道如何将返回的随机密码调用为asiggn密码(在构造函数中)。我该怎么做?您只需在构造函数中调用方法。您的程序中将使用两个方法。第一个方法检查密码是否强。如果密码强,您只需打印它。您的第二个方法生成随机密码并将其返回给第一个方法进行检查。您只需在构造函数中调用方法。我did:
Password(int-length){Password x=new Password();this.length=length;Password=x.generatePassword(length);}
但是当我从主类调用这个构造函数时,出现了一个错误:
System.out.println(“这是一个长度为+x.Password(4)的随机密码”)
我做错什么了吗?公共类Main{public static void Main(String[]args){Password c=new Password(4);}public void generatePassword(){//generate Password here}}类密码{Password(intl){System.out.println(l);}
 char c = (char)(rnd.nextInt(128-32))+32 
 20x to get Bytes, which you interpret as ASCII. // If you're fine with ASCII.
// Call a method which would check that password is strong or weak.