题解 | #求平均数#
求平均数
https://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...... int count = 0; double sum = 0; double avg = 0; while (true) { //从键盘接收数据并存放到变量num int num = scan.nextInt(); //进行判断 if (num < 0) { break; } //用来统计输入数据的次数 count++; //从键盘输入的数据进行求和 sum += num; } //求平均数 avg = sum / count; System.out.println(String.format("%.2f", avg)); } }