检查密码

问题描述:

一些网站对密码强加了某些规则。 编写一个检查字符串是否为有效密码的方法。 假设密码规则如下:

  • 密码必须至少包含八个字符。
  • 密码仅包含字母和数字。
  • 密码必须至少包含两位数字。

编写一个程序,提示用户输入密码,如果遵循规则则显示“有效密码”,否则显示“无效密码”。

这是我到目前为止:

import java.util.*; import java.lang.String; import java.lang.Character; /** * @author CD * 12/2/2012 * This class will check your password to make sure it fits the minimum set requirements. */ public class CheckingPassword { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a Password: "); String password = input.next(); if (isValid(password)) { System.out.println("Valid Password"); } else { System.out.println("Invalid Password"); } } public static boolean isValid(String password) { //return true if and only if password: //1. have at least eight characters. //2. consists of only letters and digits. //3. must contain at least two digits. if (password.length() < 8) { return false; } else { char c; int count = 1; for (int i = 0; i < password.length() - 1; i++) { c = password.charAt(i); if (!Character.isLetterOrDigit(c)) { return false; } else if (Character.isDigit(c)) { count++; if (count < 2) { return false; } } } } return true; } } 

当我运行程序时,它只检查密码的长度,我无法弄清楚如何确保它检查字母和数字,并且密码中至少有两位数。

你几乎得到了它。 但是有一些错误:

  • 你没有迭代密码的所有字符( i < password.length() - 1错了)
  • 从数字计数1而不是0开始
  • 一旦你遇到第一个数字就检查数字的位数是否至少为2,而不是在你扫描完所有字符后检查它

如前所述,您应该首先清除所有密码字符。 计算您的数字,最后检查计数是否小于2.这是引用代码。

 if (password.length() < 8) { return false; } else { char c; int count = 0; for (int i = 0; i < password.length(); i++) { c = password.charAt(i); if (!Character.isLetterOrDigit(c)) { return false; } else if (Character.isDigit(c)) { count++; } } if (count < 2) { return false; } } return true; } 

假设有效密码具有:

  • 8个或更多字符,但不超过16个字符
  • 一个或多个大写字符
  • 一个或多个小写字符
  • 一个或多个数字
  • 一个或多个特殊字符(如$,@或!)

码:

 import java.util.Scanner; public class Password { public static void main(String[] args) { // TODO Auto-generated method stub int min =8; int max=16; int digit=0; int special=0; int upCount=0; int loCount=0; String password; Scanner scan = new Scanner(System.in); System.out.println(" Enter Your Password:"); password = scan.nextLine(); if(password.length()>=min&&password.length()<=max){ for(int i =0;i=33&&c<=46||c==64){ special++; } } if(special>=1&&loCount>=1&&upCount>=1&&digit>=1){ System.out.println(" Password is good:"); } } if(password.length()0){ System.out.println(" Password must be atleat "+min+" characters:"); System.out.println(" You need atleast one upper case chracter:"); System.out.println(" You need atleast one digit:"); System.out.println(" You need atleast one special chracter:"); } } else if(password.length()1){ for(int i =0;i0&&upCount>0){ System.out.println(" Password must be atleast "+min+" chracters:"); System.out.println(" You need atleast one digit:"); System.out.println(" You need atleast one special chracter:"); } } if(password.length()>max||password.length()>=max&&upCount>1&&loCount>1&&digit>1){ System.out.println(" Password is too long.Limit is "+max+" chracters:"); System.out.println(" You need atleast one special chracter:"); } if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit>0&&special==0){ System.out.println(" You need atleast a special chracter"); } if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit==0&&special==0){ System.out.println(" You need atleast one digit:"); System.out.println(" You need atleast one special chracter:"); } } } 
 package com.parag; /* * @author Parag Satav */ public boolean check(String password) { boolean flagUppercase = false; boolean flagLowercase = false; boolean flagDigit = false; boolean flag = false; if (password.length() >= 10) { for (int i = 0; i < password.length(); i++) { if (!Character.isLetterOrDigit(password.charAt(i))) { return false; } if (Character.isDigit(password.charAt(i)) && !flagDigit) { flagDigit = true; } if (Character.isUpperCase(password.charAt(i)) && !flagUppercase) { flagUppercase = true; } if (Character.isLowerCase(password.charAt(i)) && !flagLowercase) { flagLowercase = true; } } } if (flagDigit && flagUppercase && flagLowercase) { flag = true; System.out.println("Success.."); } else System.out.println("Fail.."); return flag; }