第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在JAVA中生成一個強制一個特殊字符,一個大寫,一個小寫的密碼?

如何在JAVA中生成一個強制一個特殊字符,一個大寫,一個小寫的密碼?

泛舟湖上清波郎朗 2021-12-10 12:30:00
假設(shè)我將輸入?yún)?shù)視為int并且我想以字符串形式返回。我怎樣才能做到這一點?每次生成密碼后,我都需要強制一個特殊字符,一個大寫,一個小寫。我怎樣才能做到這一點?我在下面寫了這段代碼,我得到了一個解決方案,但它不會每次都在生成的輸出中添加大寫、小寫和特殊字符。public int GeneratePassword(int length) {    String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu vwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?";    String pwd = RandomStringUtils.random(length , characters );    System.out.println("pwd:="+pwd );    return 0;}
查看完整描述

3 回答

?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

這是使用SecureRandom和的一種方法StringBuilder


private static String generateRandomString(int length)

{

    StringBuilder sb = new StringBuilder();

    SecureRandom rnd = new SecureRandom();

    String uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    String lowercaseChars = "abcdefghijklmnopqrstuvwxyz";

    String specialChars = "0123456789~`!@#$%^&*()-_=+[{]}\\\\|;:\\'\\\",<.>/?";


    boolean nextIsUppercase = false;

    boolean nextIsLowercase = false;

    boolean nextIsSpecial = true;


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


        if (nextIsSpecial) {

            sb.append(specialChars.charAt(rnd.nextInt(specialChars.length())));

            nextIsUppercase = true;

            nextIsSpecial = false;

            continue;

        }


        if (nextIsUppercase) {

            sb.append(uppercaseChars.charAt(rnd.nextInt(uppercaseChars.length())));

            nextIsUppercase = false;

            nextIsLowercase = true;

            continue;

        }


        if (nextIsLowercase) {

            sb.append(lowercaseChars.charAt(rnd.nextInt(lowercaseChars.length())));

            nextIsLowercase = false;

            nextIsSpecial = true;

            continue;

        }


    }


    return sb.toString();

}

示例輸出:


System.out.println(generateRandomString(1)); -> 7

System.out.println(generateRandomString(2)); -> :Q

System.out.println(generateRandomString(3)); -> 8St

System.out.println(generateRandomString(4)); -> =Lv%

System.out.println(generateRandomString(16)); ->  %Uf-Hs<Ea|Wp;Rt}


查看完整回答
反對 回復 2021-12-10
?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

這是您問題的解決方案:


public static String GeneratePassword(int length) {


    String[] characters = {"0123456789","~!@#$%^&*()-_=+[{]}|;:\'\",<.>/?","ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"};

    Random rand = new Random();

    String password="";

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

            int random = rand.nextInt(4);//choose a number from 0 to 3(inclusive)

            int string_size = characters[random].length();//get length of the selected string

            int random1 = rand.nextInt(string_size);//choose a number from0 to length-1 of selected string

            char item = characters[random].charAt(random1);//selects the character

            password=password+item;//Concatenates with the password

        }

    return password;

}


查看完整回答
反對 回復 2021-12-10
?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

何塞馬丁內(nèi)斯已經(jīng)提供了一個很好的方法來實現(xiàn)這是他方法的代碼片段


   public static String getPassword(int length) {

        assert length >= 4;

        char[] password = new char[length];


        //get the requirements out of the way

        password[0] = LOWERCASE[rand.nextInt(LOWERCASE.length)];

        password[1] = UPPERCASE[rand.nextInt(UPPERCASE.length)];

        password[2] = NUMBERS[rand.nextInt(NUMBERS.length)];

        password[3] = SYMBOLS[rand.nextInt(SYMBOLS.length)];


        //populate rest of the password with random chars

        for (int i = 4; i < length; i++) {

            password[i] = ALL_CHARS[rand.nextInt(ALL_CHARS.length)];

        }


        //shuffle it up

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

            int randomPosition = rand.nextInt(password.length);

            char temp = password[i];

            password[i] = password[randomPosition];

            password[randomPosition] = temp;

        }


        return new String(password);

    }


查看完整回答
反對 回復 2021-12-10
  • 3 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號