public static boolean login(int[] message){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账号:");
int id = scanner.nextInt();
int i = 0;
for(;i<message.length;i++){//TODO:
if(message[i] == id){
return true;
}
}
return false;
}
public static boolean register(int[] message){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账号:");
int id = scanner.nextInt();
//是否之前存在id,存在,注册失败
for(int i=0;i<message.length;i++){//TODO:
if(message[i] == id){
return false;
}
}
//有效个数:TODO:
//100000000 1 2 3 4 5 99 9 9 9 9
for(int i=0;i<message.length;i++){//TODO:
if(message[i] == 0){
message[i] = id;//找到合适位置放id
return true;
}
}
return false;
}
public static void main(String[] args) {
int[] message = new int[10];
boolean flag=false;
while (true) {
System.out.println("1.登陆 2注册 3退出");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if (choice == 3) {
return;
}
switch (choice) {
case 1:
flag = login(message);
if(flag) {
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
break;
case 2:
flag = register(message);
if(flag) {
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}
break;
default:
System.out.println("输入错误");
}
}
}
}
【java】简单的整型注册登录功能的实现
最新推荐文章于 2023-02-11 14:02:00 发布