【发布时间】:2015-12-29 01:25:01
【问题描述】:
这会很快,我必须编写一个程序来检查密码是否有效。它必须至少有 8 个字符,一个小写字符,一个大写字符,至少一个特殊字符才有效。否则密码无效。一切似乎都很好,但是即使它是有效的,它似乎也永远不会结帐。我觉得它与角色位置或其他东西有关,但我无法确定它到底是什么。 编辑:更新以包括正则表达式 代码如下:
/* Class: CS1301
* Section: 9:30
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 6
* Program: 3
* ProgramName: PasswordTest
* Purpose: The program prompts the user to input a password and says if it is valid or invalid
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input the password
* Output(s): The output will be valid or invalid
* Methodology: The program will use loops to determine if valid or invalid
*
*/
import java.lang.*;
import java.util.*;
public class PasswordTest
{
public static void main (String[] args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
String pass;
int i;
boolean valid=false;
Scanner scan = new Scanner(System.in); //Initializes the scanner
//"^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$" //RegexChecker
/******************************************************************************
* Inputs Section *
******************************************************************************/
System.out.print("Please input a password: ");
pass = scan.nextLine();
/****************************variables********************************/
//*********************Using while loop so in processing*********************//
/******************************************************************************
* Processing Section *
******************************************************************************/
for (i = 0; i<pass.length(); i++)
{
if(pass.matches("^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$")){
valid=true;
}
}
if (valid==true){
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Valid!");
}
else{
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Invalid!");
}
/******************************************************************************
* Outputs Section *
******************************************************************************/
//*********************The outputs are in the processing*****************//
} //Ends string
} //Ends program
【问题讨论】:
标签: java loops passwords boolean