码迷,mamicode.com
首页 > 编程语言 > 详细

java-------注册,登录超过3次后程序退出

时间:2017-11-20 21:56:35      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:学习记录

/**实现注册,和登录*/
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次后程序退出

标签:学习记录

原文地址:http://13478818.blog.51cto.com/13468818/1983598

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!