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