利用Java提供的异常处理机制实现以下功能:

利用Java提供的异常处理机制实现以下功能:

检查用户输入的密码是否满足:

一、长度大于6

二、必须包含数字、大写字母和小写字母

//package text2;

//import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public int flag;

	public void regist(String s) throws Exception {
		int len = s.length();
		int i, f1 = 0, f2 = 0, f3 = 0, f4 = 0;
		if (len > 6) {
			f4 = 1;
		}
		for (i = 0; i < s.length(); i++) {
			if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
				f1 = 1;
			} else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
				f2 = 1;
			} else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
				f3 = 1;
			}
		}
		if (f4 == 0)
			throw new length("密码长度需要大于6!");
		else if (f1 == 0 || f2 == 0 || f3 == 0)
			throw new secretwords("你的密码长度符合要求,但你的密码必须包含数字,小写和大写字母这三部分!");
		System.out.println("密码符合正确规定");

	}

	class length extends Exception {
		private static final long serialVersionUID = 1L;

		public length(String s) {
			super(s);
		}
	}

	class secretwords extends Exception {
		private static final long serialVersionUID = 1L;

		public secretwords(String s) {
			super(s);
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		try {
			Main a = new Main();
			a.regist(s);
		} catch (Exception e) {
			if (e instanceof length) {
				e.printStackTrace();
			}
			if (e instanceof secretwords) {
				e.printStackTrace();
			}
		}
		sc.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值