英文:
While loops always returns true in java
问题
我的 while 循环无论输入是否为真都返回 true。因此,它使用 dao 中的 id 方法来检查导演是否存在。一旦这个条件变为 false 并且 id 不存在,循环就应该结束。id 从第一次迭代的 1 开始。dFName 和dSName 接受 id 为 1 的第一个 id。如果当前导演的名字与正在创建的新导演不相等,它应该将 id 增加 1。如果它们不匹配,它应该简单地返回 "true"。如果是 else 选项,那么应该再次将 id 增加 1 并返回循环。这应该一直持续到不存在第一个 Id 或找到重复的名字。
在我看来,它似乎没有运行第一个 if 语句中的条件,直接跳到嵌套在第一个 if 语句内部的 if 语句。我已经尝试过改变循环结构,使用 do... while(),只使用 if 语句,以及没有 if 的 while 语句,但没有任何变化。
这是我正在处理的循环:
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
while(directorDao.existsById(id) == true) {
dFName = directorDao.getDirectorFirstNameById(id);
dSName = directorDao.getDirectorSurnameById(id);
if (!director.getDirectorFirstName().equals(dFName) && !director.getDirectorSurname().equals(dSName)) {
id++; // 移动到下一个 Id
System.out.println(id);
} else {
if (director.getDirectorFirstName().equals(dFName) && director.getDirectorSurname().equals(dSName)) {
return true;
} else {
id++;
}
}
}
return false;
}
希望这有助于您解决问题。
英文:
My while loop returns true regardless of if he input is true or not. So it check if the director exists using the id method in dao. The while loop should end once this changes to false and the id does not exist. id starts at 1 for first iteration. d F Name and d S Name take in the first id of 1. If the current director names aren't equal to the new director being created, it should increment id by 1. If they don't not match, it should simply return "true". If its the else option then that
should just increment id by 1 again and go back into the loop. This should continue until the first Id that does not exist, or a duplicate name is found.
It seems to me that it isn't running if statement conditional in the very first if statement and skipping straight to the if statement nested inside the first. I've tried chaging around the loop, using do... while(), only if statements and while statements without ifs and no changes.
This is the loop I am working on.
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
while(directorDao.existsById(id)==true) {
dFName = directorDao.getDirectorFirstNameById(id);
dSName = directorDao.getDirectorSurnameById(id);
if (!director.getDirectorFirstName().equals(dFName) && !director.getDirectorSurname().equals(dSName)){// if directorFirstName is not equal to dFName AND directorSurname is not equal to dsname
id++; //move to next Id
System.out.println(id);
}
else {
if (director.getDirectorFirstName().equals(dFName) && director.getDirectorSurname().equals(dSName)) {
return true;
}
else {
id++;
}
}
}
return false;
}
答案1
得分: 0
逻辑分析后,你的 if-else 语句中有些是多余的。最好删除一些语句,并将其改为以下代码,这只是从你的代码中进行了简单的修改。
另外,你的变量 id
没有终止条件,这可能会导致无限循环。
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
while(directorDao.existsById(id)) {
dFName = directorDao.getDirectorFirstNameById(id);
dSName = directorDao.getDirectorSurnameById(id);
if (director.getDirectorFirstName().equals(dFName) && director.getDirectorSurname().equals(dSName)) {
return true;
}
else {
id++;
System.out.println(id);
// 在这里添加条件以避免无限循环
if (/*与 id 相关的条件*/) {
break;
}
}
}
return false;
}
英文:
Analyzing logically, something in your if-else statements are redundant. It is better to remove some statements and change to the following code which is just made simple modification from yours.
In addition, your variable id
does not have an end condition, which could lead to an infinite loop.
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
while(directorDao.existsById(id)) {
dFName = directorDao.getDirectorFirstNameById(id);
dSName = directorDao.getDirectorSurnameById(id);
if (director.getDirectorFirstName().equals(dFName) && director.getDirectorSurname().equals(dSName)) {
return true;
}
else {
id++;
System.out.println(id);
// add condition here to void an infinite loop
if (/*something to do with id*/) {
break;
}
}
}
return false;
}
答案2
得分: 0
我会重新编写整个方法。如我的评论所述,如果id为1的导演不存在,你的while循环将永远不会执行。同样,如果你有从1到x-1的id,id为x的导演将不会与任何导演关联,并且从x+1开始,你会有一些导演(即如果id不都是连续的),最后一个导演将永远不会被检查。
我想要做的是:
- 使用
directorDao
中的新方法,将所有导演检索到一个列表中,比如说directorDao.list()
- 遍历该列表。
代码示例:
public boolean checkDirectorNameIsNotDuplicate(Director director) {
List<Director> directors = directorDao.list(); // 在directorDao中添加的新方法
if (directors != null) {
for (Director d : directors) {
if (director.getDirectorFirstName().equals(d.getDirectorFirstName()) &&
director.getDirectorSurname().equals(d.getDirectorSurname())) {
return true;
} // 结束if
} // 结束for
return false;
} // 结束方法
}
此外,为什么不直接在SQL中进行检查,比如在dao中添加一个方法checkDirector(name, surname)
,如果找到具有给定姓名和姓氏的导演,则返回true?要执行的查询非常简单。
英文:
I would rewrite the entire metohd. As written in my comment, if a director with id=1 doesn't exist, your while never enters. In the same way if you have ids till x-1, id x is not associated to any director and from x+1 on you have some directors (i.e. if ids are not all contiguos), the last one will be never checked.
What i would like to do is:
- retrieve all directors in a list with a new method in directorDao, say
directorDao.list()
- iterate over the list.
Code sample
public boolean checkDirectorNameIsNotDuplicate(Director director) {
int id = 1;
String dFName;
String dSName;
List<Director> directors=directorDao.list(); //new method in directorDao
if (directors!=null){
for(Director d:directors){
if (director.getDirectorFirstName().equals(d.getDirectorFirstName()) && director.getDirectorSurname().equals(d.getDirectorSurname())) {
return true;
} //closes if
}// closes for
return false;
} //end method
Moreover, why don't you make the check directly in SQL, something like adding in the dao a method checkDirector(name, surname)
which returns true if sa director with name and surname is found ? The query to do is very easy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论