java實現注冊登錄系統
更新時間:2022年04月25日 11:01:16 作者:Carl_蔡先生
這篇文章主要為大家詳細介紹了java實現注冊登錄系統,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現注冊登錄系統的具體代碼,供大家參考,具體內容如下
1、創(chuàng)建菜單,注冊,登錄,退出
2、注冊模塊:
a) 通過鍵盤輸入用戶名,密碼
b) 保存用戶名密碼到user.txt文件(包含用戶名和密碼)
c) 注冊成功
3、登錄模塊
a) 通過鍵盤輸入用戶名和密碼
b) 判斷(超過三次提示過多錯誤,需要休眠30秒)
c) 登陸成功
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.Scanner; class TestRegex{ ?? ?public boolean isUser(String user) { ?? ??? ?String regex="[1-9][0-9]{4,9}"; ?? ??? ?boolean b=user.matches(regex); ?? ??? ?return b; ?? ?} ?? ?public boolean isMiMa(String mm) { ?? ??? ?String regex="\\w+(\\.*\\w)"; ?? ??? ?boolean b=mm.matches(regex); ?? ??? ?return b; ?? ?} } public class MySQLregisterTest{ ?? ?//1.?? ?注冊登錄系統 ?? ?//1.?? ?創(chuàng)建菜單,注冊,登錄,退出 ?? ?public static void MySQLmenu() { ?? ??? ?System.out.println("***************************"); ?? ??? ?System.out.println("*****MySQL注冊登錄系統*****"); ?? ??? ?System.out.println("**1.注冊"); ?? ??? ?System.out.println("**2.登錄"); ?? ??? ?System.out.println("**3.退出"); ?? ?} ?? ?//2.?? ?注冊模塊: ?? ?//a)?? ?通過鍵盤輸入用戶名,密碼 ?? ?//b)?? ?保存用戶名密碼到user.txt文件(包含用戶名和密碼) ?? ?//c)?? ?注冊成功 ?? ?public static void MySQLregister() throws IOException { ?? ??? ?TestRegex tr=new TestRegex(); ?? ??? ?File f=new File("user.txt"); ?? ??? ?Scanner sc=new Scanner(System.in); ?? ??? ?System.out.println("歡迎來到注冊界面!"); ?? ??? ?System.out.println("請輸入用戶名!"); ?? ??? ?String s=sc.next(); ?? ??? ?boolean bu=tr.isUser(s); ?? ??? ?FileInputStream fis=new FileInputStream("user.txt"); ?? ??? ?Properties pro=new Properties(); ?? ??? ?pro.load(fis); ?? ??? ?String user=pro.getProperty("user"); ?? ??? ?String pass=pro.getProperty("pass"); ?? ??? ?if(bu==false&&user.equals(s)) { ?? ??? ??? ?System.out.println("賬號注冊失敗"); ?? ??? ?}else { ?? ??? ??? ?FileOutputStream fos=new FileOutputStream(f,true); ?? ??? ??? ?byte[] bye=new byte[512]; ?? ??? ??? ?int len=0; ?? ??? ??? ?fos.write(("user="+s+"\r\n").getBytes()); ?? ??? ??? ?fos.flush(); ?? ??? ??? ?fos.close(); ?? ??? ??? ?fis.close(); ?? ??? ??? ?System.out.println("注冊成功"); ?? ??? ?} ?? ??? ?System.out.println("請輸入密碼!"); ?? ??? ?String st=sc.next(); ?? ??? ?boolean bm=tr.isMiMa(st); ?? ??? ?if(bm==false&&pass.equals(st)) { ?? ??? ??? ?System.out.println("密碼注冊失敗"); ?? ??? ?}else { ?? ??? ??? ?FileOutputStream fos=new FileOutputStream(f,true); ?? ??? ??? ?byte[] bye=new byte[512]; ?? ??? ??? ?int len=0; ?? ??? ??? ?fos.write(("pass="+st+"\r\n").getBytes()); ?? ??? ??? ?fos.flush(); ?? ??? ??? ?fos.close(); ?? ??? ??? ?fis.close(); ?? ??? ??? ?System.out.println("賬號注冊成功"); ?? ??? ?} ?? ?} ?? ?//3.?? ? 登錄模塊 ?? ?//a)?? ?通過鍵盤輸入用戶名和密碼 ?? ? ?? ?public static boolean Login() throws IOException{ ?? ??? ?boolean flag=false; ?? ??? ?Scanner sc=new Scanner(System.in); ?? ??? ?System.out.println("請輸入用戶名:"); ?? ??? ?String s=sc.next(); ?? ??? ?FileInputStream fis=new FileInputStream("user.txt"); ?? ??? ?Properties pro=new Properties(); ?? ??? ?pro.load(fis); ?? ??? ?String user=pro.getProperty("user"); ?? ??? ?String pass=pro.getProperty("pass"); ?? ??? ?if(s.equals(user)) { ?? ??? ??? ?System.out.println("請輸入密碼:"); ?? ??? ?} ?? ??? ?String ms=sc.next(); ?? ??? ?if(ms.equals(pass)) { ?? ??? ??? ?System.out.println("登錄成功"); ?? ??? ??? ?flag=true; ?? ??? ?} ?? ??? ?return flag; ?? ?} ?? ?//b)?? ?判斷(超過三次提示過多錯誤,需要休眠30秒) ?? ?//c)?? ?登陸成功 ?? ?public static void Oder() { ?? ??? ?int n = 1; ?? ??? ?abc: while (n <4) { ?? ??? ??? ?try { ?? ??? ??? ??? ?boolean flag = Login(); ?? ??? ??? ??? ?if (flag == false) { ?? ??? ??? ??? ??? ?n++; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?System.out.println("賬號或密碼錯誤,請確認賬號密碼"); ?? ??? ??? ??? ??? ?n = 4; ?? ??? ??? ??? ??? ?break abc; ?? ??? ??? ??? ?} ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?public static void main(String[] args) throws IOException, Exception { ?? ??? ?boolean flag=true; ?? ??? ?while(flag) { ?? ??? ??? ?MySQLmenu(); ?? ??? ??? ?Scanner sc=new Scanner(System.in); ?? ??? ??? ?System.out.println("請輸入選擇項:"); ?? ??? ??? ?int n=sc.nextInt(); ?? ??? ??? ?switch(n) { ?? ??? ??? ?case 1: ?? ??? ??? ??? ?MySQLregister(); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?Oder(); ?? ??? ??? ??? ?System.out.println("輸入次數達到上限,休眠30秒"); ?? ??? ??? ??? ?Thread.sleep(30000); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 3: ?? ??? ??? ??? ?System.out.println("已退出系統"); ?? ??? ??? ??? ?flag=false; ?? ??? ??? ??? ?break; ?? ??? ??? ?default: ?? ??? ??? ??? ?System.out.println("輸入異常!請重新輸入"); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java為什么使用BlockingQueue解決競態(tài)條件問題面試精講
這篇文章主要為大家介紹了java為什么使用BlockingQueue解決競態(tài)條件問題面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10