FOR循环中的布尔值 [英] Boolean in FOR loop
本文介绍了FOR循环中的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在循环中找到?
int id = input.nextInt();
boolean found = false;
for(int i = 0; i< z&!found; i ++){
if(arr [i] .getId()== id){
found =真正;
index = i;
$ div class =h2_lin>解决方案
只要在数组中找到输入的id,Boolean就用来终止循环。好的是编译器不必搜索,直到找到最后一个索引id。
I don't understand this code. One of my friend thought me to use the boolean to make it work. I don't really understand when he explains it. Why is the found
in the for
loop?
int id = input.nextInt();
boolean found = false;
for (int i = 0; i < z && !found; i++){
if (arr[i].getId() == id){
found = true;
index = i;
}
}
解决方案
Here "Boolean" is using to terminate the loop as soon as the inputted "id" found in your array. The good thing is the compiler don't have to search until last index wheres "id" already found.
这篇关于FOR循环中的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文