简体   繁体   English

for 循环中的 boolean return 语句和 if 语句在 java 中总是返回 false

[英]The boolean return statement inside a for loop and if statement is always returning false in java

I am trying to return a boolean output from the function "checkUser" and then to print an output for that.我正在尝试从 function “checkUser”返回 boolean output,然后打印 Z78E66621F6398FCE41 的 Z78E6DZ81。 But for every run, it is always returning 'false' even though the conditions are correct.但是对于每次运行,即使条件正确,它也总是返回“false”。 I have used an object to save the boolean result because the return statement is not working inside the 'if'.我使用了 object 来保存 boolean 结果,因为 return 语句在“if”中不起作用。 Also, I am getting the prompt- 'method breakpoint' in 'getCheck()' function.另外,我在'getCheck()'function中得到提示-'方法断点'。 Is there any thing wrong I am doing?我做错了什么吗?

import java.util.Scanner;
class myclass {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Bank[] bk=new Bank[4];
        System.out.println("Enter the Name, Age and the Acc No. of 3 users : ");
        for(int i=0; i<3; i++) {
        //System.out.print("Enter the Name : ");
        String name=sc.next();
        //System.out.print("Enter the age : ");
        int age=sc.nextInt();
        //System.out.print("Enter the Account Number : ");
        int acc_no=sc.nextInt();
        bk[i]=new Bank(name, age, acc_no);
        }
        System.out.println("Enter the Account no to validate a user : ");
        int acc_chk=sc.nextInt();
        
        // method calling
        boolean c=checkUser(bk, acc_chk);
        
        // Outputs
        if(c)
        System.out.println("The user account exits!");
    else
        System.out.println("The user account does not exit!");


}
    // method to check the authenticity of the user
    public static boolean checkUser(Bank[] obj, int ac_no) {

        //boolean ch;
        int j=0;
        for(int i=0; i<3; i++) {
            if(ac_no==obj[i].getAcc()) {
                obj[i].setCheck(true);
                j=i;
            }

        }
        return obj[j].getCheck();
        }
        

    

}
class Bank{
    String name;
    int age;
    int acc;
    String type;
    boolean check;
    
    public Bank(String s, int a, int b) {
        
    }
    public void setName(String s) {this.name=s;}
    public void setAge(int a){this.age=a;}
    public void setAcc(int b){this.acc=b;}
    public void setType(String t) {this.type=t;}
    public void setCheck(boolean ch){this.check=ch;}
    
    public String getName() {return name;}
    public int getAge() {return age;}
    public int getAcc() {return acc;}
    public String getType() {return type;}
    public boolean getCheck() {return check;}
}

Thanks for helping me, everything was right and was working perfectly fine except the constructor Bank .感谢您帮助我,一切正常,除了构造函数Bank之外一切正常。 It was not calling the setters so values were not stored in any variable and that's why boolean variable had the default value false which was returned by the function checkUser .它没有调用设置器,因此值没有存储在任何变量中,这就是为什么 boolean 变量具有由 function checkUser返回的默认值false的原因。 Thanks @FedericoklezCulloca, I tried to print the intermediate values but nothing was printing so when I went to check the constructor I found the fault.感谢@FedericoklezCulloca,我尝试打印中间值,但没有打印任何东西,所以当我去检查构造函数时,我发现了错误。

public Bank(String s, int a, int b) {
            this.setName(s);
            this.setAge(a);
            this.setAcc(b);
            
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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