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

编写一个整数数组,为数组赋值,统计其中正整数个数
编写一个整数数组,为数组赋值,统计其中正整数个数,用java写,谢谢

------解决方案--------------------
Java code

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);
    }
}