在java中打破循环

时间:2014-01-01 20:37:54

标签: java arrays login

我需要帮助,我已经厌倦了所有我可以打破循环但它一直显示打印出来的else语句。我试图找出如何通过数组登录,我没有成功。 SAD。
主要方法登录

 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 );


}
}

enter image description here

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 ;
}
}

5 个答案:

答案 0 :(得分:9)

也许不使用break语句,而是使用额外的布尔标志。

//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);

答案 1 :(得分:2)

对于“DB”中的每个人,您说如果用户名和密码不正确,则表示用户输入了错误的凭据。那是错的。在说“你有没有尝试”之前,你必须搜索整个数据库。

您的代码应为

    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");
    }

或者,如果您不喜欢它:

    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");
    }

答案 2 :(得分:1)

这只是你的循环逻辑被打破了。看起来你要做的是循环遍历people数组,如果没有元素具有指定的登录名,那么在else中执行你所拥有的操作。但是你的循环实际上做的是为数组中的每个元素执行else,而不是指定的登录(直到找到正确的登录)。

我认为你要找的是这样的:

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");
}

或者类似地,您可以将它放在自己的方法中以使逻辑更简单(但您必须更改people因此它是静态字段而不是在main中声明的变量或为其添加参数) :

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'
}

然后你说:

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");
}

答案 3 :(得分:0)

您的方法 getFname getPassword 必须首先返回person [0]的名字和密码,然后返回person [1]的用户名和密码。你想要做的是创建一个方法,检查用户名或密码是否匹配两个人对象中的任何一个,例如;

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;
}

答案 4 :(得分:0)

问题在于Person数组people的所有成员。由于它遍历所有元素,因此它将首先首先检查Heather Ward的名称和密码,因此您将点击其他内容。您可以将if-else构造更改为:

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!
}

当然,当您输入正确的名字但密码不正确时,这只会减少尝试次数(并显示警告),但这只是一个起点。