题解 | #求平均数#
求平均数
http://www.nowcoder.com/practice/41e59cee1221424bb9291435aae79ae9
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//write your code here......
//输出格式为:System.out.println(String.format("%.2f",avg));
int count=0;
int sum=0;
//注意连续要用的变量放循环外面
while(true){
int a=scan.nextInt();
if(a>0){
sum+=a;
count++;
}else{
//注意强转一个(float)
double avg=sum/(double)count;
System.out.println(String.format("%.2f",avg));
break;
}
}
// return -1; }
}