密码验证合格程序

来源:互联网 发布:身材知乎 编辑:程序博客网 时间:2024/05/16 20:29

题目描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行


输出描述:

如果符合要求输出:OK,否则输出NG

输入例子:
021Abc9000021Abc9Abc1021ABC9000021$bc9000
输出例子:
OKNGNGOK题目:http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841?tpId=37&tqId=21243&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking我的解法:
import java.util.Scanner;/** *  * @author Mouse * */public class Main {static int upperNum = 0;// 大写字母static int lowerNum = 0;// 小写字母static int intNum = 0; // 数字static int count = 0;//合计大写+小写+数字static int otherNum = 0;//其它字符的个数public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {String str = sc.nextLine();if (str.length() > 8 && just(str) && judge(str)) {System.out.println("OK");upperNum = 0;lowerNum = 0;otherNum = 0;intNum = 0;count = 0;} elseSystem.out.println("NG");upperNum = 0;lowerNum = 0;otherNum = 0;intNum = 0;count = 0;}}/** * 2、包括大小写字母.数字.其它符号,以上四种至少三种 *  * @param str * @return */private static boolean just(String str) {for (int i = 0; i < str.length(); i++) {// 判断有没大写字母if ((int) str.charAt(i) > 64 && (int) str.charAt(i) < 91) {upperNum = upperNum + 1;}// 判断有没小写字符if ((int) str.charAt(i) > 96 && (int) str.charAt(i) < 123) {lowerNum = lowerNum + 1;}// 判断有没有数字if ((int) str.charAt(i) > 47 && (int) str.charAt(i) < 58) {intNum = intNum + 1;}}otherNum = str.length() - upperNum - lowerNum - intNum;//其它字符的个数if (upperNum == 0 && lowerNum == 0 || upperNum == 0 && intNum == 0|| upperNum == 0 && otherNum == 0 || lowerNum == 0&& intNum == 0 || lowerNum == 0 && otherNum == 0 || intNum == 0&& otherNum == 0) {return false;} else {return true;}}/** * 3、不能有相同长度超2的子串重复 * @param str * @return */public static boolean judge(String str) {        int length = str.length();        for (int i = 0; i < length; i++) {            for (int j = i + 3; j < length; j++) {                String substr1 = str.substring(i, j);                String substr2 = str.substring(j);                if (substr2.contains(substr1)) {                    return false;                }            }        }        return true;    }}
运行效果:


 
1 0
原创粉丝点击