简体   繁体   中英

break for loop in java

I need help, I have tired all I can to break the loop but it keeps displaying the else statement print out. I am trying to figure out how to make a log in through an array and I have not been successful. SAD.
main method login

 import java.util.*;


 public class LogIn {

public static void main(String[] args) {
    Person [] people = new Person[2];
    people[0] = new Person("Heather","Ward","Davis");
    people[1] = new Person("Thomas","Cummings","Tomc84");
    Scanner input = new Scanner(System.in);
    String current_login = "";
    String pass = "";
    int login_count = 3;


    //do{
        System.out.print("Enter your name: ");
        current_login = input.nextLine();
        System.out.print("\nEnter your password: ");
        pass = input.nextLine();
        outerloop:
        for (Person p: people){
            if(current_login.equals(p.getF_name())  && pass.equals(p.getPassword())){
                System.out.println("\nHello " + p.getF_name() + " " + p.getL_name());
                break outerloop;
            }
            else{
                login_count--;
                System.out.println("\nYou have " + login_count + " tries");
            }
        }

    //}while(login_count > 0 );


}
}

在此处输入图片说明

public class Person {
private String f_name = "";
private String l_name = "";
private String password = "";

public Person(){};

public Person(String f_name, String l_name, String password) {
    this.f_name = f_name;
    this.l_name = l_name;
    this.password = password;
}
public String getF_name() {
    return f_name;
}
public void setF_name(String f_name) {
    this.f_name = f_name;
}
public String getL_name() {
    return l_name;
}
public void setL_name(String l_name) {
    this.l_name = l_name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public String toString(){
    return "first name: " + f_name +
            "Last name: " + l_name ;
}
}

Maybe instead of mucking around with the break statement, use an extra boolean flag.

//Change if block to set the boolean flag
if(current_login.equals(p.getF_name())  && pass.equals(p.getPassword())){
                System.out.println("\nHello " + p.getF_name() + " " + p.getL_name());
                authenticated = true;
}

//Use it in your while statement
while(login_count > 0 && !authenticated);

For each person in your "DB" you are saying that, if the username and password are not correct, means that the user entered wrong credentials. That's wrong. You have to search the entire DB before saying "You have n tries".

Your code should be

    boolean found = false;
    for (Person p: people){
        if(current_login.equals(p.getF_name())  && pass.equals(p.getPassword())){
            System.out.println("\nHello " + p.getF_name() + " " + p.getL_name());
            found = true;
            break;
        }
    }

    if(!found){
        login_count--;
        System.out.println("\nYou have " + login_count + " tries");
    }

Or, if you don't like it:

    boolean found = false;
    for (Person p: people){
        if(current_login.equals(p.getF_name())  && pass.equals(p.getPassword())){
            found = true;
            break;
        }
    }

    if(found){
        System.out.println("\nHello " + p.getF_name() + " " + p.getL_name());
    }
    else{
        login_count--;
        System.out.println("\nYou have " + login_count + " tries");
    }

It's just your loop logic that's broken. It looks like what you're trying to do is loop through the people array and if no element has the specified login, do what you have in the else. But what your loop actually does is do the else for every element in the array that's not the specified login (until it finds a correct one).

I think what you are looking for is something like this:

Person user = null;
for(Person p : people) {
    if(current_login.equals(p.getF_name()) && pass.equals(p.getPassword())) {
        user = p;
        break;
    }
}

if(user != null) {
    System.out.println("\nHello " + user.getF_name() + " " + user.getL_name());

} else { // do false action when 'not found'
    login_count--;
    System.out.println("\nYou have " + login_count + " tries");
}

Or similarly, you can put this in its own method to make the logic simpler (but you would have to change people so it's a static field instead of a variable declared in main or add a parameter for it):

static Person getUserForLogin(String name, String pass) {
    for(Person p : people) {
        if(name.equals(p.getF_name()) && pass.equals(p.getPassword())) {
            return p;
        }
    }
    return null; // do false action when 'not found'
}

Then you say:

Person user = getUserForLogin(current_login, pass);
if(user != null) {
    System.out.println("\nHello " + user.getF_name() + " " + user.getL_name());

} else {
    login_count--;
    System.out.println("\nYou have " + login_count + " tries");
}

Your methods getFname and getPassword must first return the first name and password for person[0] then return the username and password for person[1]. What you want to do is create a method which checks if the username or and password matches either of the two person objects, eg;

public boolean authenticate(String username, String password) {
    for(Person person : people) {
        if(username.equals(person.getF_name) && password.equals(person.getPassword()))
            return true;
    }
    return false;
}

The problem lies with your of all of the members of the Person array people . Since it's iterating through all of the elements, it will always first check the name and password of Heather Ward first, so you'll hit the else. You could change the if-else construct to something like:

for(Person p: people)
{
    if(current_login.equals(p.getF_name()))
    {
        if(pass.equals(p.getPassword())){
            System.out.println("\nHello " + p.getF_name() + " " + p.getL_name());
            break;
        }
        else{
            login_count--;
            System.out.println("\nYou have " + login_count + " tries");
        }
    }
    // no else at this level, we just want to see if the name exists in
    // our array.
    // If not, just continue the loop, don't give an error if the current
    // Person doesn't match!
}

Of course this will only reduce the number of tries (and display the warning) when you enter a correct first name but incorrect password, but it's a starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM