首先创建一个登陆信息类
创建一个集合用来存储登陆信息的对象
调用键盘输入捕捉器Scanner
将输入的信息和集合中的每个信息对象进行比较
如果一致则登陆成功
否则登陆失败
当然还以设置登陆失败的次数
代码:
登陆信息类
package kehouzuoye22_3;
public class XinXi<A> {
private String name;
private String mima;
public XinXi() {
super();
}
public XinXi(String name, String mima) {
super();
this.name = name;
this.mima = mima;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public String toString() {
return "用户名:"+name+",密码:"+mima;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XinXi<?> other = (XinXi<?>) obj;
if (mima == null) {
if (other.mima != null)
return false;
} else if (!mima.equals(other.mima))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
验证测试类
package kehouzuoye22_3;
import java.util.ArrayList;
import java.util.Scanner;
public class DengLu {
public static void main(String[] args) {
ArrayList<XinXi<String>> arr = new ArrayList<XinXi<String>>();
XinXi<String> x = new XinXi<String>();
x.setName("张曼玉");
x.setMima("ZhangManYu");
XinXi<String> x1 = new XinXi<String>();
x1.setName("关之琳");
x1.setMima("GuanZhiLin");
arr.add(x);
arr.add(x1);
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String s2 = sc.nextLine();
System.out.println("请输入密码:");
String s3 = sc.nextLine();
XinXi<String> x2 = new XinXi<String>();
x2.setName(s2);
x2.setMima(s3);
for (int i = 0; i < arr.size(); i++) {
if(arr.get(i).equals(x2)){
System.out.println("登录成功!");
break;
}else if(i==arr.size()-1){
System.out.println("登录失败!");
}
}
for (XinXi<String> n : arr) {
System.out.println(n);
}
}
}
|
|