While循环使用不匹配的用户名/密码退出

时间:2019-01-21 23:07:56

标签: java authentication while-loop do-while

所以这只是我程序的一小部分,但实际上我想做的是在while循环中进行基本身份验证。我不确定是否应该使用while循环或其他方法。
如果用户名和密码与“帐户”匹配,我只希望程序停止循环。
我不确定这个问题是否很清楚,但这是我能做的最好的事情。如果您需要任何澄清,请告诉我。

编辑:如果我输入了错误的用户名或密码,它不会再次循环播放。那是我的问题。

这是我遇到问题的部分:

User user2 = new User();
        userList.add(user2);
        String userLogin = "estebangong23";
        String userPass = "twitterisfun";
        String existingUserName = " ";
        String existingPassWord = " ";

        user2.setUserName("estebangong23");
        user2.setEmailAddress("esteban.gong@yahoo.com");
        user2.setPassWord("twitterisfun");

        System.out.println("Please sign in here.");

        while (!existingUserName.equals("estebangong23") && existingPassWord.equals("twitterisfun")) {

            System.out.println("Username: ");
            existingUserName = sc.nextLine().toLowerCase();

            System.out.println("Enter your password: ");
            existingPassWord = sc.nextLine().toLowerCase();

            //System.out.println("Log In unsuccessful. Incorrect username or password.");
        } 

下面是我的大部分程序:

    import java.util.*;
    import java.io.*;

    public class MyClass {
    public static void main(String args[]) {
    // make it so u can log in to an existing account
    // make it double check the password and email

    Scanner sc = new Scanner(System.in);

    List<User> userList = new ArrayList<User>();
    List<String> matchesList = new ArrayList<String>();
    String date = "03/21/2019";
    String tweet = " ";

    System.out.println("-------------------------------------------------------------------------");
    System.out.println("|                               Twitter                                 |");
    System.out.println("| Sign Up                                                        Log in |");
    System.out.println("-------------------------------------------------------------------------");

    System.out.println("Would you like to Log In or Sign Up?");
    String responseString = sc.nextLine().toLowerCase();


    if (responseString.equals("signup") ) {

        User user1 = new User(); //this creates the first user

        System.out.println("Alright, lets get started by setting up your profile!");

        System.out.println("Please enter a username.");
        user1.setUserName(sc.nextLine());

        System.out.println("Please enter your email address.");
        user1.setEmailAddress(sc.nextLine());

        System.out.println("Please enter a password.");
        user1.setPassWord(sc.nextLine());

        System.out.println("Alright we got your account all setup. Just take a moment to review everthing. Don't worry you can change this stuff later if you want!");
        user1.printUserProfile();

        userList.add(user1);

        System.out.println("Your account is all setup, go ahead and try posting something for the first time!");


        } else { //end signup if

        User user2 = new User();
        userList.add(user2);
        String userLogin = "estebangong23";
        String userPass = "twitterisfun";
        String existingUserName = " ";
        String existingPassWord = " ";

        user2.setUserName("estebangong23");
        user2.setEmailAddress("esteban.gong@yahoo.com");
        user2.setPassWord("twitterisfun");

        System.out.println("Please sign in here.");

        while (!existingUserName.equals("estebangong23") && existingPassWord.equals("twitterisfun")) {

            System.out.println("Username: ");
            existingUserName = sc.nextLine().toLowerCase();

            System.out.println("Enter your password: ");
            existingPassWord = sc.nextLine().toLowerCase();

            //System.out.println("Log In unsuccessful. Incorrect username or password.");
        } 

        } //end else

    //main program
    System.out.println("-------------------------------------------------------------------------");
    System.out.println("Let's post something! Show the world what your thinking about righ now!");
    System.out.println("If you want to stop entering tweets at any time, please enter '**'. ");

    System.out.println(" ");

    while (!tweet.equals("**")) {

        System.out.println("Enter your tweet here: ");
        tweet = sc.nextLine().toUpperCase();

        String[] tweetArray = tweet.split(" ");

        System.out.println("");




    } //end tweet while loop


    System.out.println("-------------------------------------------------------------------------");

    } //end main

    } //end main class

    class User {
    // this class sets up the users profile when they are signing up
private String userName;
private String emailAddress;
private String passWord;
public List<Tweet> tweetList = new ArrayList<Tweet>();

//Username, Age, Email Address, Tweets[ ]
//Methods
//Setters and getters, Create tweet
public User () {

}

public String getUserName() {return this.userName;}
public void setUserName(String userName) {this.userName = userName;}

public String getEmailAddress() {return this.emailAddress;}
public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}

public String getPassWord() {return this.passWord;}
public void setPassWord(String passWord) {this.passWord = passWord;}

public void printUserProfile() {
    System.out.println("Username: " + this.userName);
    System.out.println("Email Address: " + this.emailAddress);
    System.out.println("Password (remember not to share this to anyone) " + this.passWord);
}

}

1 个答案:

答案 0 :(得分:1)

要简单地回答您的问题,

while (! (existingUserName.equals(userLogin) && existingPassWord.equals(userPass)) ) { // if users doesn't exist
...

}

但是,您需要重新考虑用于身份验证的代码。我猜你才刚刚开始。首先将它们放在表中,然后使用PreparedStatement访问它们。