1. 程式人生 > >java-------註冊,登錄超過3次後程序退出

java-------註冊,登錄超過3次後程序退出

學習記錄

/**實現註冊,和登錄*/
public class Login {

    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);
        System.out.println("********用戶註冊*********");
        System.out.println("請輸入用戶名:");
        String username=in.nextLine();
        System.out.println("請輸入密碼:");
        String password=in.nextLine();
        
        //保持輸入的用戶名和密碼
        Users u = new Users(username,password);
        Login login=new Login();
        login.setUser(u);
        System.out.println("註冊成功!");
        
        System.out.println("********用戶登錄*********");
        int iCount=0;//登錄次數
        do{
            System.out.println("請輸入用戶名:");
            username=in.nextLine();
            
            System.out.println("請輸入密碼:");
            password=in.nextLine();
            if(login.userLogin(username,password)){
                System.out.println("登錄成功");
                break;
            }else{
                iCount ++; 
                if(iCount >= 3 && login.userLogin(username,password) == false){
                    System.out.print("登陸失敗次數超過三次,程序即將退出!");
                    System.exit(0);
                }else{
                    System.out.println("請檢查用戶名與密碼");
                    }
                }
            }while(true);    
    }    
      
     
    //判斷用戶名和密碼是否正確
    public  boolean userLogin(String username,String password){
        
        if(this.user.uesrname.equals(username) && this.user.password.equals(password)){
            return true;
        }else{
            return false;
        }    
    }
    
    Users user;
    public void setUser(Users user){
        this.user=user;
    }
}

//用戶類
class Users{
    
    String uesrname;
    String password;
    
    //構造方法
    public Users(String username,String password){
        this.uesrname=username;
        this.password=password;
    }
    
}
===================實現結果========================
********用戶註冊*********
請輸入用戶名:
zhangsan
請輸入密碼:
11
註冊成功!
********用戶登錄*********
請輸入用戶名:
zhangsan
請輸入密碼:
12
請檢查用戶名與密碼
請輸入用戶名:
zhangsan
請輸入密碼:
11
登錄成功

java-------註冊,登錄超過3次後程序退出