带for循环的Java布尔方法

带for循环的Java布尔方法,java,arrays,for-loop,boolean,Java,Arrays,For Loop,Boolean,嘿,我试图在一个方法中做一个for循环,它应该返回一个布尔值。然而,我不断得到一个错误。基本上,数组可以变得非常大,我希望通过整个数组来检查用户名和密码 public class Users { private String username; private String password; private String[][] accounts = { { "user1", "pass1" }, { "user2", "pass2" } }; public

嘿,我试图在一个方法中做一个for循环,它应该返回一个布尔值。然而,我不断得到一个错误。基本上,数组可以变得非常大,我希望通过整个数组来检查用户名和密码

public class Users {
    private String username;
    private String password;
    private String[][] accounts = { { "user1", "pass1" }, { "user2", "pass2" } };

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

    public boolean check() {

        for (int i = 0; i < accounts.length; i++) {
            if ((username.equals(accounts[i][0])) && (password.equals(accounts[i][1]))) 
                return true;
            else 
                return false;
        }
    }

}
公共类用户{
私有字符串用户名;
私有字符串密码;
私有字符串[][]帐户={{“user1”、“pass1”}、{“user2”、“pass2”};
公共用户(字符串用户名、字符串密码){
this.username=用户名;
this.password=密码;
}
公共布尔检查(){
对于(int i=0;i
如果
帐户
数组中的第一个条目与
用户名
密码
不匹配,则当前返回的是
false

如果要检查所有帐户,应仅在循环完成后返回
false

public boolean check() {
    for (int i = 0; i < accounts.length; i++) {
        if ((username.equals(accounts[i][0])) && (password.equals(accounts[i][1]))) 
            return true;
    }
    return false;
}
公共布尔检查(){
对于(int i=0;i
您的for循环应该是反向的,请查看下面的内容

public class Users {
private String username;
private String password;
private String[][] accounts = { { "user1", "pass1" }, { "user2", "pass2" } };

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

public boolean check() {

    for (int i = 0; i < accounts.length; i++) {
        if ((!username.equals(accounts[i][0]))||(password.equals(accounts[i][1]))) 
            return false;

    }
 return true;
} 

}
公共类用户{
私有字符串用户名;
私有字符串密码;
私有字符串[][]帐户={{“user1”、“pass1”}、{“user2”、“pass2”};
公共用户(字符串用户名、字符串密码){
this.username=用户名;
this.password=密码;
}
公共布尔检查(){
对于(int i=0;i
好吧,让我们谈谈为什么? 你说有时候你会有一个大数组,所以如果一个数组出错,就没有必要继续循环,如果有错误的数据,它会立即返回false,否则它会返回true

更新 若你们的循环和你们做的一样,你们要检查第一个结果是否正确 这就是为什么我反转了循环

boolean method(){//请注意:当调用此方法时
boolean method() { // please note: when this method is called

  int i; // it executes this line

    for(i=0; i<10; i++){  // then it executes this line
       // while this loop is executing it also executes return false statement
        if(i==8){
           return true;
        }
    }

     return false;// then it executes this line

//therefore the boolean value will be false. i mean the returned boolean value and //then it returns true;
}
int i;//它执行这一行
对于(i=0;i您不会一直收到“错误”。您遇到了一个非常具体的错误。如果您对编程非常不熟悉,以致于不理解错误,并且将其视为噪音,这并不意味着它不重要。实际上,这是目前为止最重要的事情。发布它。为了澄清问题,
return
语句立即中断了它们使用的方法e in,因此当for循环将
用户名
密码
与第一个
字符串[]进行比较时
帐户中,无论值是否相等,循环都会中断,方法会返回。事实上,没有,您甚至没有收到错误。您的代码只是没有按照预期的方式工作。@MikeNakis我相信这就是他们所说的“收到错误”他们的代码没有给他们想要的东西。更不用说使用并行数组了……您将
返回true;
放在
检查()之外
method。这在语法上是不正确的。在这种情况下,如果第一个结果正确而不检查其他数据,它将返回true\@basilbatikhi我相信这是必需的行为。一旦找到匹配项,就不需要检查数组的其余部分。如果要求数组的所有元素都匹配,则需要数组的所有元素不是完全相同的,这意味着您根本不需要数组。这取决于需求,所以我们都有正确的答案