英文:
The boolean return statement inside a for loop and if statement is always returning false in java
问题
尝试从函数 "checkUser" 返回一个布尔输出,然后打印相应的输出。但是每次运行时,即使条件正确,它总是返回 'false'。我使用了一个对象来保存布尔结果,因为在 'if' 内部返回语句不起作用。
此外,在 'getCheck()' 函数中出现了提示 "method breakpoint"。我是否有什么错误?
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++) {
String name = sc.next();
int age = sc.nextInt();
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();
// 调用方法
boolean c = checkUser(bk, acc_chk);
// 输出
if (c)
System.out.println("The user account exists!");
else
System.out.println("The user account does not exist!");
}
// 检查用户的真实性方法
public static boolean checkUser(Bank[] obj, int ac_no) {
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) {
name = s;
age = a;
acc = 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; }
}
英文:
I am trying to return a boolean output from the function "checkUser" and then to print an output for that. But for every run, it is always returning 'false' even though the conditions are correct. I have used an object to save the boolean result because the return statement is not working inside the 'if'.
Also, I am getting the prompt- 'method breakpoint' in '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;}
}
答案1
得分: 0
谢谢你的帮助,除了构造函数 Bank
外,一切都是正确的,而且一切都运行得很好。构造函数没有调用设置器,所以值没有存储在任何变量中,这就是为什么布尔变量的默认值是 false
,这个值被 checkUser
函数返回。谢谢 @FedericoklezCulloca,我尝试打印中间值,但什么都没有打印出来,所以当我去检查构造函数时,我找到了错误。
public Bank(String s, int a, int b) {
this.setName(s);
this.setAge(a);
this.setAcc(b);
}
英文:
Thanks for helping me, everything was right and was working perfectly fine except the constructor 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
. 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.
public Bank(String s, int a, int b) {
this.setName(s);
this.setAge(a);
this.setAcc(b);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论