检查数组中的所有值 [英] Checking all values in an array

查看:45
本文介绍了检查数组中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数字数组:

I have an array of numbers like this:

int array[5];

我正在尝试打破while循环。我基本上想说一下数组中的所有数字是否都大于100,然后结束循环。

I am trying to break out of a while loop. I basically want to say if all numbers in the array are greater than 100 then end the loop.

有没有一种处理方法?

推荐答案

您可以检查数组中的每个元素,如果只有小于100的元素则中断。

You can check every element in the array and break if only the element is smaller than 100.

在循环外,根据 i -循环计数器-是否等于大小来打印消息。如果相等,则所有元素均大于所有元素,否则其中一个较小:

And outside the loop print the message based on whether i - The loop counter- is equal to the size or not. If equal than all elements are greater otherwise one of them is smaller:

int array[] = {271, 120, 469, 77, 2000};
//int array[] = {271, 120, 469, 787, 2000}; // uncomment this line and comment out the line above to see the difference.


int i = 0; 
for(; i < 5; i++)
    if(100 > array[i])
        break;

if(i != 5)
    std::cout << "Not all the values are greater than 100" << std::endl;
else
    std::cout << "All the values are greater than 100" << std::endl;

这篇关于检查数组中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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