1. Program Design Idea Verification Code
① defines a string variable to hold randomly generated.
② uses loops to generate six-bit random numbers that are converted to char types and written behind strings when each bit is generated.
③ uses the dialog box to display the generated verification code and prompts the user to enter a verification code.
④ shows whether the user verified success.
2. Program Flowchart
3. SOURCE program code
ImportJava.util.Random;ImportJavax.swing.JOptionPane;//Import Class Joptionpane Public classVerificationcode {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubString code= "";//to define a new empty string for(inti=1;i<=6;i++) {Random random=NewRandom (); intCharacter=random.nextint (26) +97;//randomly generates a number between 97-123Code=code+ (Char) character;//Converts the resulting number to a char type lowercase letter} String Input=joptionpane.showinputdialog (code+ "\ n" + "Please enter the Verification Code:");//Display the verification code, and prompt the user to enter a verification code if(Input.equals (code))//determine if the verification code is correct{Joptionpane.showmessagedialog (NULL, "Verification code is correct, verify success!" "," Results ", Joptionpane.plain_message); } Else{Joptionpane.showmessagedialog (NULL, "Verification code error, Verification failed!" "," Results ", Joptionpane.plain_message); } }}
4. Results
5. Experimental summary
The key is to grasp the type conversion, there are a lot of methods to generate random strings, to examine the use of Randomstr.java and type conversion. I am judging the randomly generated string and the user entered the verification code is the same error, my code is code==input, the occurrence of whatever the input verification code will show verification success, and then through the query, found to be judged error, because in C + + directly judge the line, In Java, you use the Equals function to judge. The second mistake was carelessness, and I wrote input.equals (code) as input.equals (input), and the verification was successful no matter what code was entered.
In addition, one of the drawbacks of the program is that it can only produce all lowercase letters or all capital letters, or all of the numbers of the verification code, can not produce lowercase, uppercase letters, the number of random combination of verification code.
Requires a six-bit validation string to be generated from the Randomstr.java: using type conversion, and each time the sample program runs, a different string is generated.