Java註冊登錄學習筆記

import java.util.Scanner;
java


//用戶類ide

class Usersthis

{spa

String username;it

String password;class

public Users(String username,String password) {import

this.username = username;程序

this.password = password;密碼

}方法

}


public class LoginDome {

Users user;

//判斷用戶名與密碼

public  boolean userLogin(String username,String password)

{

if(this.user.username.equals(username) && this.user.password.equals(password))

{

return true;

}

else

{

return false;

}

}

public void setUser(Users user)

{

this.user = user;


public static void main(String[] args) {

String username;

String password;

int iCount = 0;  //計數器判斷登錄幾回

boolean flag = false;//判斷是否登錄成功

Scanner in = new Scanner(System.in);

System.out.println("***********用戶註冊************");

System.out.println("請輸入用戶名:");

username = in.nextLine();

System.out.println("請輸入密碼:");

password = in.nextLine();

//保持輸入的用戶與密碼

Users u = new Users(username,password);

LoginDome demo = new LoginDome();

demo.setUser(u);

System.out.println("註冊成功!");

System.out.println("***********用戶登錄************");

do{

System.out.println("請輸入用戶名:");

username = in.nextLine();

System.out.println("請輸入密碼:");

password = in.nextLine();

if(demo.userLogin(username,password))  //調用判斷是否登錄成功方法

{

break;

}

else

{

iCount ++; //記錄次數

if(iCount >= 3 && flag == false)  //若是登錄失敗超過3次

{

System.out.print("登錄失敗次數超過三次,程序即講退出!");

System.exit(0);

}

else

{

System.out.println("請檢查用戶名與密碼");

}

}

}while(true);

}

}

***********用戶註冊************

請輸入用戶名:

liwen

請輸入密碼:

123456

註冊成功!

***********用戶登錄************

請輸入用戶名:

li

請輸入密碼:

222

請檢查用戶名與密碼

相關文章
相關標籤/搜索