import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
loop0:
while(true){
System.out.println("1.注册 2.登录 3.退出:");
if(sc.hasNextInt()){//为了安全的获取一个int值;
int op = sc.nextInt();
switch(op){
case 1://注册
loop1:
do{
System.out.println("请输入注册名:");
String uName = sc.next();
System.out.println("请输入密码:");
String uPwd = sc.next();
if(uName.trim().length() == 0 || uPwd.trim().length() == 0){
System.out.println("请输入注册名和密码!");
continue loop1;
}
//验证登录名是否重复
for(User u : userList){
if(u.getName().equals(uName)){
System.out.println("此名称已经被注册,请重新注册!");
continue loop1;
}
}
//可以注册
User user = new User(uName,uPwd);
//添加到集合
userList.add(user);
System.out.println("注册成功!");
continue loop0;
}while(true);
case 2://登录
do{
System.out.println("请输入登录名:");
String uName = sc.next();
System.out.println("登录密码:");
String uPwd = sc.next();
//封装User对象
User user = new User(uName,uPwd);
//判断
for(User u : userList){
if(user.equals(u)){
System.out.println("登录成功!");
//猜数字小游戏
continue loop0;
}
}
System.out.println("用户名或密码错误,请重新登录!");
}while(true);
case 3://退出
System.out.println("谢谢使用,拜拜!");
System.exit(0);//结束JVM
break;
default:
System.out.println("请输入:1,2,3的数字选项!");
}
}
}
}
}
|
|