从数组登录Java用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19726180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java username and password login from array
提问by Alexander Hansen
Ok. I am making a piece of software for administration use on a computer. Enabling people to log in and do stuff dependong on their clearance. What i have so far is an array system of all users with ther username, password and clearance. What i want to be able to do is from a java swing window(which i have already programmed in) and compare username and password from that window to all the arrays and check for a match. any help will be greatly appreciated. thanks. My code:
好的。我正在制作一款用于在计算机上进行管理的软件。使人们能够登录并根据他们的许可进行操作。到目前为止,我拥有的是一个包含用户名、密码和许可的所有用户的阵列系统。我想要做的是从一个 java swing 窗口(我已经在其中编程)并将该窗口中的用户名和密码与所有数组进行比较并检查是否匹配。任何帮助将不胜感激。谢谢。我的代码:
public class Main {
public static void main(String[]args){
WindowLogin.openWindow();
}
}
public class Database {
public static String[] userAdmin = {"admin","admin","10"}; //Username password clearance
public static String[] userAlexander = {"Alexander","1234","1"}; //Same stuff here
}
import javax.swing.*;
import java.awt.*;
public class WindowLogin {
public static void openWindow(){
JFrame f = new JFrame("Login");
JTextField userName = new JTextField(20);
JPasswordField password = new JPasswordField();
JPanel info = new JPanel(new GridLayout(2,1,5,5));
JPanel lnPanel = new JPanel(new GridLayout(1,1,5,5));
JButton lnButton = new JButton("Log in");
info.add(userName);
info.add(password);
lnPanel.add(lnButton);
f.setLayout(new FlowLayout());
f.setResizable(false);
f.add(info);
f.add(lnPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
采纳答案by Kashif Nazar
This is how you can achieve this.
这就是您可以实现这一目标的方法。
String inputUserName = userName.getText(); //assign the user's input username
String inputPassword = password.getText(); //assign the user's input password
if(inputUserName.equals(userAdmin[0]) && inputPassword.equals(userAdmin[1])){
System.out.println("Welcome Admin!");
}else if(inputUserName.equals(userAlexander [0]) && inputPassword.equals(userAlexander[1])){
System.out.println("Welcome Alexander!");
}else{
System.out.println("Unauthorized access");
}
On a different note, it is recommended that you create a User type to bundle the user-related information instead of using arrays.
另一方面,建议您创建一个 User 类型来捆绑用户相关信息,而不是使用数组。
回答by user902383
First, create object user
一、创建对象用户
class User{
String username;
String password;
boolean isAdmin;
public User(String username, String password,boolean isAdmin){
this.username=username;
this.password=password;
this.isAdmin=isAdmin;
}
//setters and getters;
}
second, place your users not in list, but in map Map<String,User> userMap
where key will be username, so when sb will log to the system,
其次,将您的用户放在列表中,而不是在地图中Map<String,User> userMap
,其中键将是用户名,以便当 sb 登录系统时,
public int logIn(String username, String password)
{
User user = userMap.get(username);
if (user==null
{
//No User with given username, return error code 1;
return 1;
}
if (password==user.getPassword().equals(password))
{
//password correct, error code - no error
return 0;
}
//password incorrect return error code 2
return 2
}
now all what we need is to init your database
现在我们需要做的就是初始化你的database
class Database {
Map<String,User> userMap = new HashMap();
{
User admin = new User("admin","admin",true);
User alex= new User("Alexander","1234",false);
userMap.put(admin.getName(),admin);
userMap.put(alex.getName(),alex);
}
}