Java生成随机密码(包含大写、小写、数字、特殊字符)

前言

【本文旨在为没有思路或没有什么思路的提供参考】
见过某个笔试题:生成一个不少于6位的随机密码,要求含有大写字母、小写字母、数字、特殊字符中的三种。

分析一波

思路: 两个重点,一个是随机,一个是4类中必须含有3种,因此我的思路是,首先定义一个包含所有字符的字符串,再在字符串中随机取出字符进行拼接,最后对拼接的字符串进行检查(符合即退出,不符合再重新取、重新拼接)。
PS: 如果你的需求是至少1种,那就简单了,直接取就是了。

	public static String getPsw(int len) {
		// 1、定义基本字符串baseStr和出参password
		String password = null;
		String baseStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|<>?";
		boolean flag = false;
		// 2、使用循环来判断是否是正确的密码
		while (!flag) {
			// 密码重置
			password = "";
			// 个数计数
			int a = 0,b = 0,c = 0,d = 0;
			for (int i = 0; i < len; i++) {
				int rand = (int) (Math.random() * baseStr.length());
				password+=baseStr.charAt(rand);
				if (0<=rand && rand<=9) {
					a++;
				}
				if (10<=rand && rand<=35) {
					b++;
				}
				if (36<=rand && rand<=61) {
					c++;
				}
				if (62<=rand && rand<baseStr.length()) {
					d++;
				}
			}
			// 3、判断是否是正确的密码(4类中仅一类为0,其他不为0)
			flag = (a*b*c!=0&&d==0)||(a*b*d!=0&&c==0)||(a*c*d!=0&&b==0)||(b*c*d!=0&&a==0);
		}
		return password;
	}

结果: 生成例如一个6位和10位的
在这里插入图片描述在这里插入图片描述

分析

问题描述: 发现有个问题,无论密码要求为多少位,for循环都会执行完,而不管是否已经不符合。如:若密码要求10位,前面四次循环把4类的雨露均沾了,那么按道理后面6次就应该不循环,浪费时间【虽然这个没啥影响,也就毫秒级的时间而已】。
如何解决: 可以在for循环内加一个if判断,若乘积不为0,就跳出for,即:

if (a * b * c * d != 0) {
	break;
}

完整示例:

/**
 * < 一句话描述这个类的作用 >
 * @date 2019年8月28日 下午4:56:15
 * @author *******
 */
package com.gxs.psw;

public class CreatPsw {
	/**
	 * <一句话描述此方法的作用>
	 * @date 2019年8月28日 下午5:01:30
	 * @param index
	 * @return String
	 */
	public static String getPsw(int len) {
		// 1、定义基本字符串baseStr和出参password
		String password = null;
		String baseStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|<>?";
		boolean flag = false;
		// 2、使用循环来判断是否是正确的密码
		while (!flag) {
			// 密码重置
			password = "";
			// 个数计数
			int a = 0, b = 0, c = 0, d = 0;
			for (int i = 0; i < len; i++) {
				int rand = (int) (Math.random() * baseStr.length());
				password += baseStr.charAt(rand);
				if (0 <= rand && rand < 10) {
					a++;
				}
				if (10 <= rand && rand < 36) {
					b++;
				}
				if (36 <= rand && rand < 62) {
					c++;
				}
				if (62 <= rand && rand < baseStr.length()) {
					d++;
				}
				if (a * b * c * d != 0) {
				    break;
			    }
			}
			// 是否是正确的密码(4类中仅一类为0,其他不为0)
			flag = (a * b * c != 0 && d == 0) || (a * b * d != 0 && c == 0) || (a * c * d != 0 && b == 0)
					|| (b * c * d != 0 && a == 0);
		}
		return password;
	}
	
	/**
	 * <一句话描述此方法的作用>
	 * @date 2019年8月28日 下午4:56:15
	 * @param args
	 * @return void
	 */
	public static void main(String[] args) {
		System.out.println(CreatPsw.getPsw(10));
	}
}


再BB两句

刚开始我甚至是以这种思路写的:定义四个集合,分别是大小写数字和字符,然后每次随机选择其中一类(当然不会重复选择),在每次随机选择的集合中,再随机取一定长度的字符,最后拼接。比如取6位:第一次随机,选择了数字,那就随机取1 ~ 6-1-1个数字(比如随了3个),第二次随机,选择了大写,那就随机取1 ~ 6 -1-3个大写(比如随了1个),第三次随机,选择了特殊字符,那就直接取6-3-1个。后来想了想,这样只能是局部随机,不是整体随机,即使再打乱一下,也显得臃肿,贼麻烦,相似代码多。所以还是要思路清晰,才能体现出效率!!!

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以使用Java的Random类来生成随机的字符串,然后使用Java中的StringBuilder类来拼接字符串,设置需要的大写字母、小写字母以及数字,最后使用StringBuilder类中的toString()方法将生成的密码转换成字符串。 ### 回答2: 要用Java制作密码的自动生成器,可以按照以下步骤实现: 1. 首先,定义一个包含所有大写字母、小写字母和数字的字符集合。 ```java String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; ``` 2. 确定密码的长度,并创建一个空字符串来存储生成的密码。 ```java int length = 8; // 密码长度为8 String password = ""; ``` 3. 使用循环生成每个密码字符。在每次循环中,随机选择字符集合中的一个字符,并将其添加到密码字符串中。 ```java for (int i = 0; i < length; i++) { int index = (int) (Math.random() * characters.length()); password += characters.charAt(index); } ``` 4. 确保密码中至少包含一个大写字母、一个小写字母和一个数字。可以使用布尔变量来记录是否满足此要求。 ```java boolean hasUppercase = false; boolean hasLowercase = false; boolean hasNumber = false; ``` 5. 再次使用循环检查密码中是否包含所需的字符类型,并根据需要进行修改。如果缺少某种字符类型,则随机选择一个字符,替换密码中的一个字符。 ```java for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (Character.isUpperCase(c)) { hasUppercase = true; } else if (Character.isLowerCase(c)) { hasLowercase = true; } else if (Character.isDigit(c)) { hasNumber = true; } } if (!hasUppercase) { int index = (int) (Math.random() * 26); password = password.substring(0, index) + characters.charAt(index) + password.substring(index + 1); } if (!hasLowercase) { int index = 26 + (int) (Math.random() * 26); password = password.substring(0, index) + characters.charAt(index) + password.substring(index + 1); } if (!hasNumber) { int index = 52 + (int) (Math.random() * 10); password = password.substring(0, index) + characters.charAt(index) + password.substring(index + 1); } ``` 6. 最后,输出生成的密码。 ```java System.out.println("生成的密码为:" + password); ``` 通过以上步骤,我们可以使用Java制作一个密码的自动生成器,要求密码大写字母、小写字母和数字组成。 ### 回答3: 要用Java制作密码的自动生成器,首先需要定义密码的规则和长度。根据题目要求,密码需要由大写字母、小写字母和数字组成。具体实现步骤如下: 1. 定义密码的长度,例如设定密码长度为8位。 2. 创建一个包含大写字母、小写字母和数字的字符数组,例如: ```java char[] characters = {'A', 'B', 'C', ..., 'a', 'b', 'c', ..., '0', '1', '2', ...}; ``` 3. 使用Math.random()方法生成随机索引,从字符数组中随机选择一个字符,并将它添加到密码的字符串中,重复该步骤直到密码长度达到设定值。 ```java StringBuilder password = new StringBuilder(); for (int i = 0; i < length; i++) { int randomIndex = (int) (Math.random() * characters.length); password.append(characters[randomIndex]); } ``` 4. 最后得到的password即为满足要求的密码。 通过以上步骤,我们可以使用Java制作一个简单的密码自动生成器,生成包含大写字母、小写字母和数字密码,且满足指定的长度要求。需要注意的是,密码生成器的复杂程度可以根据需求进行调整,例如可以添加特殊字符、要求特定的密码模式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值