我想将用户名“carl”与密码“0001”以及其他元素匹配,但它不起作用。任何人都可以解释我如何将“用户名”的每个元素与“密码”的元素相匹配?非常感谢你! 这是我的代码:
class log{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String [] username ={"carl", "may", "joseph", "oliver", "ashley"};
String []password={"0001", "0002", "0003", "0004", "0005"};
public void login() throws IOException{
System.out.print("\nUSERNAME: ");
uname=br.readLine();
System.out.print("PASSWORD: ");
pword=br.readLine();
for (j = 0; j <= 4; j++) {
if (username[j].equals(password[j])) {
uname=username[j];
pword=password[j];
}
}
while((!uname.equals(password[j]))&&(!pword.equals(username[j]))){
System.out.println("Invalid username/password.");
System.out.print("\nUSERNAME: ");
uname=br.readLine();
System.out.print("PASSWORD: ");
pword=br.readLine();
}
public void main(String[] args)throws IOException {
log b=new log();
b.login();
}
答案 0 :(得分:0)
我认为你应该按照以下方式修改你的方法
http://localhost:8084/mavenwebproject/XmlServletForm.html
答案 1 :(得分:0)
我认为使用额外的课程User
会更好。
该课程如下:
class User {
private String name;
private String pw;
public User (String name, String pw) {
this.name = name;
this.pw = pw;
}
public String getName() {
return this.name;
}
public String getPw() {
return this.pw;
}
}
在您的程序逻辑中,您有一个包含所有用户的列表:
ArrayList<User> userList
现在您已更改登录算法:
class log{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
ArrayList<User> userList = ...; //Here you have to add all users first
boolean loginCheck = false;
while(loginCheck == false) {
System.out.print("\nUSERNAME: ");
uname = br.readLine();
System.out.print("PASSWORD: ");
pword = br.readLine();
for (int i = 0; i < userList.size(); i++) {
if (userList.get(i).getName.equals(uname) && userList.get(i).getPw.equals(pword)) {
loginCheck = true;
break;
}
}
}
}
public void main(String[] args)throws IOException {
log b=new log();
b.login();
}
如果将用户输入的检查更改为:
,则可以使算法更快for (int i = 0; i < userList.size(); i++) {
if (userList.get(i).getName.equals(uname)) {
if (userList.get(i).getPw.equals(pword)) {
loginCheck = true;
break;
} else {
break;
}
}
}
答案 2 :(得分:0)
您可以使用OOP(面向对象编程)来更轻松地解决您的问题:
以下是我的示例解决方案:
用户类:
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
User user2 = (User) o;
if (this.username.equals(user2.getUsername()) && this.password.equals(user2.getPassword()))
return true;
return false;
}
}
登录课程:
public class Login {
private User[] users = new User[]{
new User("carl", "0001"),
new User("may", "0002"),
new User("joseph", "0003"),
new User("oliver", "0004"),
new User("ashley", "0005"),
};
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nUSERNAME: ");
String uname = br.readLine();
System.out.print("PASSWORD: ");
String pword = br.readLine();
boolean success = new Login().login(new User(uname, pword));
if (success)
System.out.println("The user account is valid, login");
else
System.out.println("The username and/or password is invalid.");
}
public boolean login(User user) {
for (int i = 0; i < users.length; i++) {
if (users[i].equals(user))
return true;
}
return false;
}
}