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.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.