读书人

编写一个整数数组为数组赋值统计其

发布时间: 2012-03-30 17:32:09 作者: rapoo

编写一个整数数组,为数组赋值,统计其中正整数个数
编写一个整数数组,为数组赋值,统计其中正整数个数,用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);    }} 

读书人网 >J2ME开发

热点推荐