日期:2014-05-20  浏览次数:20716 次

编写一个整数数组,为数组赋值,统计其中正整数个数
编写一个整数数组,为数组赋值,统计其中正整数个数,用java写,谢谢
------解决方案--------------------

public class GetCount {

/**
 * @param args
 */
public static void main(String[] args) {
//这里以随便生成的数来判断正整数的个数
int[] arr = new int[6];
Random r = new Random();
int count = 0;//统计正整数个数
System.out.print("数组中的数为:");
for(int i=0; i<6; i++) {
arr[i] = r.nextInt();//给数组赋值
System.out.print(arr[i] + "   ");
}

//计算正正数的个数
for(int i=0; i<6; i++) {
if(arr[i]>0)
count++;
}
System.out.println();
System.out.println("正整数的个数为:" + count);
}
}


因为是随机生成的数,所以每次运行结果是不一样的,其结果可能为:
数组中的数为:1572484213   442406701   622125904   1374838202   -446848513   -546900968   
正整数的个数为:4