C#数组的使用

来源:百度知道 编辑:UC知道 时间:2024/05/31 05:35:29
有这样一个数组
int[] arr1 = new int[] { 12,4,22,5,9,36,7,14,2,18};
我怎么统计其中大于30数的个数并赋值给n
大虾帮帮忙

int[] arr1 = new int[] { 12, 4, 22, 5, 9, 36, 7, 14, 2, 18 };
int n=0;
for (int i = 0; i < arr1.Length; i++)
{
if (arr1[i]>30)
{
n++;
}
}

最后n就是其中大于30的个数

int[] arr1 = new int[] { 12, 4, 22, 5, 9, 36, 7, 14, 2, 18 };
int n=0;
string str="";
string x=0;
for (int i = 0; i < arr1.Length; i++)
{
if (arr1[i]>30)
{
str=str+",";
x=x+",";
n++;
}
}

n就是其中大于30的个数
str就是 那些大于30得数的一个','分割的字符串
x就是那些大于30得数组的下标用','分割的字符串
自己拆分一下就可以了

可以先写个方法,如下:
public int CountNumber(int[] myArr,int myNum)
{