自动收售货系统

题目描述

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

输入描述:

首先输入一个正整数n,
然后输入n个整数。

输出描述:

输出负数的个数,和所有正整数的平均值。

示例1

输入

5
1
2
3
4
5

输出

0 3.0

没什么好说的,直接上代码:
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            int n=sc.nextInt();
            int sum=0;
            int count1=0;
            int count2=0;
            for(int i=0;i<n;i++){
                int a=sc.nextInt();
                if(a<0){
                    count1++;
                }
                else if(a>0){
                    count2++;
                    sum+=a;
                }
            }
            System.out.printf("%d %.1f
",count1,(sum*1.0/count2));
            
        }
    }
}
原文地址:https://www.cnblogs.com/hisoka-study/p/13494967.html