r语言计数器

Python015

r语言计数器,第1张

定义函数oddcount,计算给定数列中的奇数的个数

对于上述案例,我们是想告诉r,我们定义了一个名叫 oddcount 函数 function(x);

该函数的自变量是x,作用是判断函数自变量 x 是不是奇数;

判断依据:n %% 2 == 1;若是,要求计数器 k 加 1 ;

要求返回的是k;

计数(1,3,5) # 可以看到共有3个奇数;

返回值为:3;

第2组数有5个,其中,2是偶数;

返回值:4;

尝试返回偶数的个数,具体过程如下:

好了,我的小伙伴们,今天就先到这儿吧,下期见!O(∩_∩)O哈哈~

public class Test {

public static int oddCount(int[] arr){

int count = 0

for(int i : arr){

if(i%2!=0){

count++

}

}

return count

}

public static void main(String[] args){

int[] arr ={0,1,3,4,5,6,7}

System.out.println(Test.oddCount(arr))

}

}