The first sample of Java

Source: Internet
Author: User

Recently learned about java,2 day and half time (April 21, 2015-April 23, 2015) completed the introduction.

Learn by writing a small sample that generates a random password. The console version was written on April 22. The morning of April 23 wrote the GUI version. This is Java getting started. April 24 wrote the android application version.

Paste the console version of the source code, I hope you can give correct.

Console version running effect:


GUI version running effect:

Console version Source code

Randompasswordsample2.java

1//by Jiurl
2//Micro BlogHTTP://WEIBO.COM/DDQQPPB
3// 
4
5ImportJava.util.Scanner;
6
7 PublicclassRandomPasswordSample2 {
8
9 PublicStaticvoidMain (string[] args) {
Ten//TODO auto-generated Method Stub
OneSystem.out.printf ("******************************\n");
ASystem.out.printf ("Random Password generator\n");
-System.out.printf ("by jiurl\n");
-System.out.printf ("******************************\n");
the
-intNpasswordlength;
-System.out.printf ("Input password length:");
-Scanner sc =NewScanner (system.in);
+Npasswordlength = Sc.nextint ();
-Sc.close ();
+
A//generate Npass Passwords one time to choose
atFinalintNpass = 10;
-randompassword[] Rndpass =NewRandompassword[npass];
-
-intI
- for(i=0; i<npass; i++)
-{
inRndpass[i] =NewRandompassword (npasswordlength);
-}
to
+//Display the results
- for(i=0; i<npass; i++)
the{
*System.out.printf ("%s\n", Rndpass[i]. Getrandompassword ());
$}
Panax Notoginseng}
-}

Randompassword.java1 //by Jiurl
2//Micro BlogHTTP://WEIBO.COM/DDQQPPB
3// 
4
5ImportJava.util.Random;
6
7 Publicclassrandompassword{
8PrivateString Strrandompassword;
9PrivateintNrandompasswordlength;
Ten
OnePrivateFinalStaticCharCaalphabet[] =
A{
-' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ',
-' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ',
the' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '
-};
-
- PublicRandompassword (intNrandompasswordlength) {
+ This. nrandompasswordlength = Nrandompasswordlength;
- This. Strrandompassword = Createrandompassword (nrandompasswordlength);
+}
A
at PrivateString Createrandompassword (intNrandompasswordlength) {
-intI
-
-Random rnd =NewRandom ();
-Rnd.setseed (System.nanotime ());
-
inStringBuilder Strbuilder =NewStringBuilder ("");
-
tointX
+x = Math.Abs (Rnd.nextint ())%26;
-
theStrbuilder.append (Randompassword.caalphabet[x]);
* for(i=1;i<nrandompasswordlength;i++)
${
Panax Notoginsengx = Math.Abs (Rnd.nextint ())%36;
-Strbuilder.append (Randompassword.caalphabet[x]);
the}
+
AStrrandompassword = Strbuilder.tostring ();
the
+return This. Strrandompassword;
-}
$
$ PublicString Getrandompassword () {
-returnStrrandompassword;
-}
the}

Weibo: HTTP://WEIBO.COM/DDQQPPB

The first sample of Java

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.