java:檢測密碼

檢測密碼:一些網站對於密碼有一些規劃。編寫一個方法,檢測字符串是否是一個有效密碼。假定密碼規則如下:

  • 密碼必須至少8位字符
  • 密碼僅能包含字母和數字
  • 密碼必須包含至少兩個數字
編寫一個程序,提示用戶輸入一個密碼,如果符合規則,顯示vailid password,否則顯示invalid password
import java.util.*;
public class five518 {
	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		System.out.print("enter the passwords: ");
		String password=input.next();
		if(password.length()>=8)
		{
			int length=password.length();
			int count=0;
			int count1=0;
			int count2=0;
			int count3=0;
			while(count<length) {
				if((password.charAt(count)<='9'&&password.charAt(count)>='0')||(password.charAt(count)<='Z'&&password.charAt(count)>='A')||(password.charAt(count)<='z'&&password.charAt(count)>='a'))
					count1++;
				count++;
			}
			if(count1==length)
			{
				while(count2<length) {
					if(password.charAt(count2)<='9'&&password.charAt(count2)>='0')
						count3++;
					count2++;
				}
				if(count3>=2)
					System.out.println("valid password");
				else
					System.out.println("invalid password");
			}
			else
				System.out.println("invalid password");
			
		}
		else
			System.out.println("invalid password");
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章