题解 | #求平均数#
求平均数
http://www.nowcoder.com/practice/41e59cee1221424bb9291435aae79ae9
import java.util.Scanner; import java.lang.String;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//write your code here......
int m = 0;
int i = 0;
//一定要是double类型,不然会发生截断,导致小数点后出错。
double sum = 0.0;
while((m = scan.nextInt()) > 0){
sum += m;
i++;
}
double avg = sum/i;
//输出格式为:System.out.println(String.format("%.2f",avg));
System.out.println(String.format("%.2f",avg));
}
}