数组——简单的登录

登录login 注册register
请选择: 1. 登录 2.注册
1
请输入用户id: 111
存在: 登录成功
不存在:打印登录失败

假设选择 2:
请输入用户id: 111
注册成功(已经注册过此时打印注册失败,否在成功)

代码如下:
public class TestDemo{

public static boolean search(int[] arr,int id){
    for(int i=0;i<arr.length;i++){
        if(arr[i] == id){
            return true;
        }
    }
    return false;
}
public static boolean login(int[] arr){
    Scanner scanner = new Scanner(System.in);
    System.out.println("输入id:");
    int id = scanner.nextInt();
    return search(arr,id);

// boolean flag = search(arr,id);
// return flag;
}
public static boolean register(int[] arr,int[] num){
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
// 1.找到显示注册失败
boolean flag = search(arr,id);
if(flag){
return false;
}
// 2.往相对应的下标放值 // 111 222
int size = num[0];//有效个数
arr[size] = id;
num[0] = num[0]+1;
return true;
}
public static void main(String[] args) {
final int INITSIZE =11;
int[] ids = new int[INITSIZE];
int[] num = new int[1];
while (true) {
System.out.println(“1.登录 2.注册 3.退出”);
Scanner scanner = new Scanner(System.in);
int chioce = scanner.nextInt();//1 2
switch (chioce) {
case 1:
boolean flag = login(ids);
if (flag) {
System.out.println(“登录成功”);
} else {
System.out.println(“登录失败”);
}
break;
case 2:
flag = register(ids, num);
if (flag) {
System.out.println(“注册成功”);
} else {
System.out.println(“注册失败”);
}
break;
case 3:
return;
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值