多个登录用户和密码Java

时间:2017-03-10 22:12:17

标签: java string if-statement

我想为具有特定密码的特定用户登录多个用户。我知道String user = new String("User")拥有用户登录名,但我希望能够添加多个名称和密码。我还想知道是否可能只有某些密码可以与某些用户一起使用。

P.S。我也是初学者,请放轻松我。

import java.io.*;

public class Login {
    public static void main(String[] args) throws IOException {

        String greeting = "Hello";
        String username;
        String password;
        String user = new String("User");
        String pass = new String("Password");


        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

        System.out.println("*** Welcome to the program ***\n");
        System.out.println(greeting);

        System.out.println("Please type your username :");
        username = br.readLine();   
        System.out.println("Please type your password :");
        password = br.readLine();


        if (username.equals(user))
            System.out.println("They are the same");
        else 
            System.out.println("That is wrong");

        System.exit(0);

        if (password.equals(pass))
            System.out.println("They are the same");
        else 
            System.out.println("That is wrong");
        System.exit(0);                 
    }
}

4 个答案:

答案 0 :(得分:1)

您需要的是一个可以迭代的用户列表,寻找匹配项。每个用户都有一个用户名和密码。我们将创建一个名为User的新类,将其自己的文件放入User.java:

public class User {
    private String username;
    private String password;

    User (String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    String getUsername() {return username;}
    String getPassword() {return password;}

}

现在,回到您编写的代码,以下内容创建了这些用户的列表,向其添加了一些虚拟用户,然后遍历该列表以查找匹配项。这应该可以帮助你。

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class JavaTest {

    public static void main(String[] args) throws IOException
    {
        String greeting = "Hello";
        String username;
        String password;

        // Used to hold the instance of a user who successfully logged in
        User loggedInUser = null;

        // Create an empty list to hold users
        List<User> listOfUsers = new ArrayList<>();

        // Add 3 users to the list
        listOfUsers.add(new User("user1","password1"));
        listOfUsers.add(new User("user2","password2"));
        listOfUsers.add(new User("user3","password3"));

        // Get user input
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

        System.out.println("*** Welcome to the program ***\n");
        System.out.println(greeting);

        System.out.println("Please type your username :");
        username = br.readLine();   
        System.out.println("Please type your password :");
        password = br.readLine();

        // Iterate through list of users to see if we have a match
        for (User user : listOfUsers)
        {
            if (user.getUsername().equals(username))
            {
                if (user.getPassword().equals(password))
                {
                    loggedInUser = user;

                    // when a user is found, "break" stops iterating through the list
                    break;
                }
            }
        }

        // if loggedInUser was changed from null, it was successful
        if (loggedInUser != null)
        {
            System.out.println("User successfully logged in: "+loggedInUser.GetUsername());
        }
        else
        {
            System.out.println("Invalid username/password combination");
        }
    }
}

答案 1 :(得分:0)

Compass说你可以使用地图。

    private static Map<String,String> passwdMap = new LinkedHashMap<>();
    static {
        passwdMap.put("kmick", "somepass123");
        passwdMap.put("gjames", "q8secret");
    }
    public boolean check(String user, String password){
        return password != null && user!=null && password.equals(passwdMap.get(user));
    }

然后是个人建议:防御性地使用,尝试使用

if(condition){
   action();
}

而不是

if(condition)
  action()

因为它&#39;以后很容易搞错更新代码。

答案 2 :(得分:-2)

您可以使用两个String数组,一个用于用户名,第二个用于用户密码。您可以使用if

代替switch n case

答案 3 :(得分:-2)

Client