登录系统
需求
实现注册,登录两个功能,可以不注册直接登录
String accounts[]=new String [5];
String passwords[]=new String [5];
accounts[0]="123";
passwords[0]="123";
Scanner sc=new Scanner(System.in);
boolean fla=false;
boolean lag=false;
System.out.println("*****登录*****");
System.out.print("是否需要注册?(y/n)");
String ch=sc.next();
这里定义了初始账号密码,可以直接进行登录,用数组来存储。
并且判断知否需要注册,从键盘输入y/n
if("y".equals(ch)){
//需要注册
for(int k=0;k<accounts.length;k++){
if(accounts[k]!=null){
System.out.print("请输入账号:");
String account=sc.next();
System.out.print("请输入密码:");
String password=sc.next();
accounts[k]=account;
passwords[k]=password;
System.out.println("注册成功");
lag=true;
break;
}else{
System.out.print("用户已满,注册失败");
}
}
}
先来看注册部分,键盘输入y表示需要注册,遍历账号密码的数组,如果有null则可以注册。
if("n".equals(ch)||lag){
//不需要注册,直接登陆
for(int k=0;k<accounts.length;k++){
do{
System.out.print("请输入账号:");
String account=sc.next();
System.out.print("请输入密码:");
String password=sc.next();
if(account.equals(accounts[k])&&password.equals(password)){
System.out.println("登录成功");
fla=true;
}else{
System.out.print("登录失败,重新登录");
}
}while(!fla);
if(fla){
break;
}
}
}
这是登录部分
两种情况的输出