您的位置:首页 > 编程语言 > Java开发

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

2017-11-20 20:27 309 查看
/**实现注册,和登录*/
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
登录成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  记录 学习