如何检查非零值(元素)的阵列 [英] How to check an array for non-zero values (elements)

查看:121
本文介绍了如何检查非零值(元素)的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何着手编写,将返回是否至少有三个值不等于0,我搜索过类似问题的功能,但我找不到任何有效的解决方案。为了说明我的问题,这里有一个例子:

I am wondering how to approach writing a function that would return whether there were at least three values not equal to 0. I've searched for similar problems, but I couldn't find any effective solution. In order to explain my problem, here is an example:

我有一个包含这些元素的数组: [1,0,2,0,4,0,0,3,0,0] 。我想,以检查是否有至少3个元素不等于零。

I have an array which contains these elements: [1,0,2,0,4,0,0,3,0,0]. I want to check if there are at least 3 elements not equal to zero.

我的code将返回 1 如果有至少3个元素!= 0 ,或返回 0 如果有少于3个元素!= 0

My code would return 1 if there are at least 3 elements != 0 , or return 0 if there are less than 3 elements !=0.

所以,在我的例子,它应该返回 1

So in my example it should return 1.

推荐答案

元素计数不等于零的数量。如果小于3,返回false,否则返回true。

Count the number of elements not equal to zero. If less than three, return false, else return true.

为了获得伯爵,遍历数组的内容使用for循环,然后使用if测试语句,如果该元素是零。

In order to obtain the count, loop through the contents of the array using a for loop and then use an if statement to test if the element is zero.

这篇关于如何检查非零值(元素)的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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