FOR循环中的布尔值 [英] Boolean in FOR loop

查看:190
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆