一个密码中包含数字,大写字母,小写字母,特殊符号@#$%^&*中的三种,长度在6-12 之间,并且不能出现连续相同的字符串;正确返回1,不正确返回0.

该博客介绍了一个Java程序,用于检查密码是否符合特定要求:包含数字、大写字母、小写字母和特殊符号@#$%^&*中的三种,长度6-12位,并且不允许连续相同字符。程序通过两个方法实现,`ContinuousEquality`检查连续相等字符,`precheck`使用正则表达式验证字符种类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.sun.DoSubject;

import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//设定一个密码,密码中包含数字,大写字母,小写字母,特殊符号@#$%^&*中的三种,
//长度在6-12 之间,并且不能出现连续相同的字符串;例如:aabcde ababcdefg,正确返回1 不正确返回0.
public class PasswordFilter {
	public static void main(String[] args) {
		PasswordFilter a = new PasswordFilter();
		String s = "ab12aa";
		System.out.println(a.CheckSetPassword(s));

	}

	public int CheckSetPassword(String s) {
		boolean result1 = precheck(s);
		boolean result2 = ContinuousEquality(s);
		if (result2 && result1) {
			return 1;
		}
		return 0;
	}

	/**
	 * 是否包含连续相等的字符串
	 * 
	 * @param s
	 * @return
	 */
	private boolean ContinuousEquality(String s) {
		Queue<String> q1 = null;
		// i为字符串分组的个数 从1 到字符串的一半
		for (int i = 1; i < s.length() / 2 + 1; i++) {
			// 先将s加入到队列q1中
			{
				q1 = new LinkedList<>();
				for (int l = 0; l < s.length(); l++) {
					q1.add(String.valueOf(s.charAt(l)));
				}
			}
			Queue<String> q = new LinkedList<String>();
			// 将分好的每组添加到队列中
			for (int j = 0; j < Math.ceil(s.length() / i); j++) {// 一组由i个元素,可以分为Math.ceil(s.length()
																	// / i)组
				// 每次添加q1队列中的几个元素,即q1弹几次
				String a = "";
				for (int j2 = 0; j2 < i; j2++) {
					if (q1.isEmpty()) {
						break;
					}
					a = a + q1.poll();

				}
				q.add(a);

			}
			// 比较队列前后的值是否相等
			String prev = "";
			String next = "";
			while (!q.isEmpty()) {

				if (prev.isEmpty()) {
					prev = q.poll();
				} else {
					prev = next;
				}
				next = q.poll();
				if (prev.equals(next)) {
					return false;
				}
			}

		}
		return true;
	}

	/**
	 * 先判断是否满足第一个条件,四种类型字符中的三种,使用正则表达式判断
	 * 
	 * @param s
	 * @return
	 */
	private boolean precheck(String s) {

		if (s.length() > 12 || s.length() < 6) {
			return false;
		}

		int a = 0;
		int b = 0;
		int c = 0;
		int d = 0;
		for (int i = 0; i < s.length(); i++) {
			if (Character.isUpperCase(s.charAt(i))) {
				a = 1;
			}
			if (Character.isLowerCase(s.charAt(i))) {
				b = 1;
			}
			if (Character.isDigit(s.charAt(i))) {
				c = 1;
			}
		}

		Pattern p = Pattern.compile("[@#$%^&*]");
		Matcher m = p.matcher(s);
		boolean match = m.find();
		if (match) {
			d = 1;
		}

		if (a + b + c + d == 3) {
			return true;
		}
		return false;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值