检查数组中的所有值

编程入门 行业动态 更新时间:2024-10-27 16:32:09
本文介绍了检查数组中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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;

更多推荐

检查数组中的所有值

本文发布于:2023-11-30 00:24:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!