如题,求解
创建一个user封装类,含有username,pwd,先创建3个对象,并初始化。模拟登陆,如果账号正确,输出welcome+用户名(要求user初始化之后,放入list。...
创建一个user封装类,含有username,pwd,先创建3个对象,并初始化。模拟登陆,如果账号正确,输出welcome +用户名(要求user初始化之后,放入list。登陆时要遍历list查找)
展开
展开全部
public static void test2() {
// 初始化User
List<User> users = new ArrayList<User>();
users.add(new User(1, "java", "java"));
users.add(new User(2, "android", "android"));
users.add(new User(3, "test", "test"));
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String pwd = sc.next();
for (int i = 0; i < users.size(); i++) {
if (username.equals(users.get(i).getUserName()) && pwd.equals(users.get(i).getPwd())) {
JOptionPane.showMessageDialog(null, "welcome " + username);
}
}
}
public class User {
private int id;
private String userName;
private String pwd;
public User(int id, String userName, String pwd) {
super();
this.id = id;
this.userName = userName;
this.pwd = pwd;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
// 初始化User
List<User> users = new ArrayList<User>();
users.add(new User(1, "java", "java"));
users.add(new User(2, "android", "android"));
users.add(new User(3, "test", "test"));
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String pwd = sc.next();
for (int i = 0; i < users.size(); i++) {
if (username.equals(users.get(i).getUserName()) && pwd.equals(users.get(i).getPwd())) {
JOptionPane.showMessageDialog(null, "welcome " + username);
}
}
}
public class User {
private int id;
private String userName;
private String pwd;
public User(int id, String userName, String pwd) {
super();
this.id = id;
this.userName = userName;
this.pwd = pwd;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//用户类
class User {
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
//登录方法
public static boolean login (String _username,String _password) {
boolean rs = false;
List<User> users = new ArrayList<User>();
users.add(new User("zhangsan","KS345"));
users.add(new User("lisi","KS1234567"));
users.add(new User("wangwu","KS12345"));
for(int i = 0; i < users.size(); i ++){
if(_username.equals(users.get(i).getUsername())
&& _password.equals(users.get(i).getPassword())){
rs = true;
break;
}
}
return rs;
}
//测试方法
public static void main(String[] args) {
if(login("zhangsan","KS123")){
System.out.println("你登录成功了!");
}else{
System.out.println("你登录失败了!");
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
//登录方法
public static boolean login (String _username,String _password) {
boolean rs = false;
List<User> users = new ArrayList<User>();
users.add(new User("zhangsan","KS345"));
users.add(new User("lisi","KS1234567"));
users.add(new User("wangwu","KS12345"));
for(int i = 0; i < users.size(); i ++){
if(_username.equals(users.get(i).getUsername())
&& _password.equals(users.get(i).getPassword())){
rs = true;
break;
}
}
return rs;
}
//测试方法
public static void main(String[] args) {
if(login("zhangsan","KS123")){
System.out.println("你登录成功了!");
}else{
System.out.println("你登录失败了!");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询