1081 检查密码 (15分) ( 正则最简方法)(Java 题解)

1081 检查密码 (15分)



原题链接:传送门

一、题目:

在这里插入图片描述

输入样例 1:
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6
输出样例 1:
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.


二、解析:

思路:

  对于每个条件直接直接使用正则匹配。
  正则非常有帮助,推荐学习。

正则表达式学习请查看我这篇:【正则表达式】总结

AC代码1:最简
import java.util.Scanner;

/**
 * 1081 检查密码 (15分)
 * 
 * @思路:直接使用正则匹配即可
 * @author: ChangSheng 
 * @date:   2019年12月30日 下午3:42:44
 */
public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		s.nextLine();
		while(N-- > 0) {
			String password = s.nextLine(), print = null;
			if (password.length() < 6 && print == null) print= "Your password is tai duan le.";
			// 没有匹配到字母、数字、点
			if (!password.matches("[a-zA-Z0-9.]+") && print == null) print = "Your password is tai luan le.";
			// 没有匹配到任意一个数字
			if (!password.matches(".*[0-9].*") && print == null) print = "Your password needs shu zi.";
			// 没有匹配到任意一个字母
			if (!password.matches(".*[a-zA-Z].*") && print == null) print = "Your password needs zi mu.";
			if (print == null) print = "Your password is wan mei.";
			System.out.println(print);
		}
	}
}
AC代码2:
import java.util.Scanner;

/**
 * 1086 就不告诉你 (15分)
 * 
 * @思路:直接使用正则匹配即可
 * @author: ChangSheng 
 * @date:   2019年12月30日 下午3:42:44
 */
public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		s.nextLine();
		while(N-- > 0) {
			String password = s.nextLine();
			if (password.length() < 6) {
				System.out.println("Your password is tai duan le.");
				continue;
			}
			// 没有匹配到字母、数字、点
			if (!password.matches("[a-zA-Z0-9.]+")) {
				System.out.println("Your password is tai luan le.");
				continue;
			}
			// 没有匹配到任意一个数字
			if (!password.matches(".*[0-9].*")) {
				System.out.println("Your password needs shu zi.");
				continue;
			}
			// 没有匹配到任意一个字母
			if (!password.matches(".*[a-zA-Z].*")) {
				System.out.println("Your password needs zi mu.");
				continue;
			}
			System.out.println("Your password is wan mei.");
		}
	}
}

相关

PAT - 乙级 - 题解集

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值