首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值,结果保留一位小数。 本题有多组输入用例。

注意里面的0的情况

在这里插入图片描述


import java.util.Scanner;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main5 {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
//        int i1 = in.nextInt();
        while (in.hasNext()&&in.nextLine()!=null) {
    
     // 注意 while 处理多个 case
//                String num = in.nextLine();
                int nCnt = 0;
                int pCnt = 0;
                int pSum=0;
//                System.out.println("num:"+num);
                String ss = in.nextLine() ;
//                System.out.println("ss:"+ss);
                String[] s = ss.split(" ");
                for(int i=0;i<s.length;i++){
    
    
                    int a = Integer.parseInt(s[i]);
                    if (a>0){
    
    
                        pCnt+=1;
                        pSum+=a;
                    }else if (a<0){
    
    
                        nCnt+=1;
                    }
                }
            float  avg=(float)pSum/pCnt;
                System.out.printf("%d %.1f\n",nCnt,avg);

            }

    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/JavaBigData/article/details/116853719