Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple login and registration with Java

2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to achieve simple login and registration in Java. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

First, login

1. Content introduction

Define a built-in account and password, write account input, password input, random CAPTCHA generation, CAPTCHA input method (3 input methods are not overloaded) and account password CAPTCHA comparison method. Call these methods to implement a simple console login.

two。 Train of thought analysis

Method call. The verification code is judged first, and the password is judged at the end of the account.

3. Code implementation

Import java.util.Random;import java.util.Scanner;public class Login {public static void main (String [] args) {/ / define the built-in account and password String account = "xiaojian"; String passwd = "123456"; String code = ""; / / New keyboard input object Scanner scan = new Scanner (System.in); Login login = new Login () / / call input account String accountinput = login.accountinput (scan); / / call input password String passwdinput = login.passwdinput (scan); / / generate code = login.code (4) by CAPTCHA; / / output CAPTCHA System.out.println ("CAPTCHA is (please note case):" + code) / / call input verification code String codeinput = login.codeinput (scan); / / call verification method login.Contrast (account,passwd,code,accountinput,passwdinput,codeinput);} / / enter public String accountinput (Scanner scan) {System.out.print ("Please enter account number:"); String accountinput = scan.next (); return accountinput } / / password input public String passwdinput (Scanner scan) {System.out.print ("Please enter password:"); String passwdinput = scan.next (); return passwdinput;} / / Random CAPTCHA generates public String code (int length) {/ / define an empty string String code = "" / / define a scanner / / for loop for (; length > 0; length--) {/ / then determine whether to generate numbers or letters / / letters? 65'90 uppercase letters 97 '122 lowercase letters Random rand = new Random (); int flag = rand.nextInt (3); / / determine whether uppercase or lowercase switch (flag) {case 0: Random random = new Random (); code + = rand.nextInt (10); break Case 1: Random random1 = new Random (); code + = (char) (rand.nextInt (26) + 65); break; case 2: Random random2 = new Random (); code + = (char) (rand.nextInt (26) + 97) Break;}} return code;} / / enter public String codeinput (Scanner scan) {System.out.print ("Please enter CAPTCHA:"); String codeinput = scan.next (); return codeinput } / / account password verification code vs. public void Contrast (String account, String passwd, String code, String accountinput, String passwdinput, String codeinput) {if (! code.equals (codeinput)) {System.out.println ("incorrect verification code"); return } if (! account.equals (accountinput)) {System.out.println ("account error"); return;} if (! passwd.equals (passwdinput)) {System.out.println ("password error"); return } System.out.println ("Hello," + "[" + account+ "]" + "welcome back!");}}

4. Running

II. Registration

1. Content introduction

Call the login method to achieve registration.

two。 Train of thought analysis

Fixed accounts are written by account writing method, and you choose to log out or log in.

3. Code implementation

! [idea64_LHWNc62sOe] (.. / screenshot file / 2021-07/idea64_LHWNc62sOe.png) import java.util.Scanner;public class Register {public static void main (String [] args) {/ / define the fixed account password and enter the account password verification code String account = "xiaojian"; String passwd = "123456"; String code; String accountinput = ""; String passwdinput = "" String codeinput = ""; Scanner scan = new Scanner (System.in); Login get = new Login (); System.out.println ("enter 1 login without 2 registration"); System.out.print ("Please enter:"); int flag = scan.nextInt () While (true) {if (flag==1) {/ / call input account accountinput = get.accountinput (scan); / / call input password passwdinput = get.passwdinput (scan); / / call verification code to generate code = get.code (4) / / output verification code System.out.println ("CAPTCHA is (please note case):" + code); / / call CAPTCHA input codeinput = get.codeinput (scan); / / call comparison method get.Contrast (account,passwd,code,accountinput,passwdinput,codeinput); break } if (flag==2) {/ / call input account account = get.accountinput (scan); / / call input password passwd = get.passwdinput (scan); System.out.println ("register successfully, please remember the account password!") ; System.out.println ("account: + account+" password is "+ passwd); System.out.println (" Please select 0 to exit 1 to log in: "); System.out.print (" Please enter: "); flag= scan.nextInt (); if (flag==0) {break }}}

4. Running

These are all the contents of the article "how to simply sign in and register with Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report