将用户信息存储在数组里,验证用户名、密码登录
首先介绍一下数组 : 数组(Array)是一组相同类型的变量的集合,可以存放一组相同类型的数据。数组需要三个要素 : 数组名、数据类型、数组长度。
数组可以定义int、char、String等多种数据类型。
简单介绍两种定义方式:
int[] array = {1,2,3,4,5} ;//数组长度为5的int型
String[] str = new String[]{"ab","cd","ef"};//数组长度为3的String型
下面和大分享一下通过数组存储用户信息实现登录
import java.util.Scanner;
public class login {
public static void main(String[] args) {
String[] name= {"zhangsan","lisi","wangwu","zhaoliu"};
String[] pwd= {"123","321","123456","654321"};
boolean chk=false;
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
int j=0;
while(j<3) {
System.out.println("请输入用户性名:");
String uName=sc.next();
System.out.println("请输入用户密码:");
String uPwd=sc.next();
for(int i=0;i<name.length;++i) {
if(uName.equals(name[i])&&uPwd.equals(pwd[i])) {
chk=true;
break;
}
}
if(chk) {
System.out.println("success,welcome...");
break;
}
else {
System.out.println("sorry,your name or password error...");
}
++j;
}
}
}
初学,会有点瑕疵漏洞,欢迎大家提出意见或建议。