题解 | #求平均数#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//write your code here......
//初始化整数和
float sum=0;
//统计个数,先初始化
int i=0;
while (true) {
int PotiveInt=scan.nextInt();
//输入后先判断是否为负数
if (PotiveInt<0) break;
i++;//满足条件,个数+1
sum+=PotiveInt;//满足条件,计算总和
}
float average=sum/i;
System.out.println(String.format("%.2f",average));//格式化输出
//输出格式为:System.out.println(String.format("%.2f",avg));
}
}