案例:利用累加器计算前N个学生的总成绩和平均成绩

 1 /*
 2  *录入N个学生的成绩,并求出这些学生的总成绩和平均成绩! 
 3  * */
 4 import java.util.Scanner; 
 5 
 6 public class SumTest{
 7     public static void main(String args[]){
 8         
 9         int i = 0;
10         int sum = 0;
11         System.out.println("请输入总学生的数量:");
12         Scanner sc = new Scanner(System.in );
13         int n = sc.nextInt();
14         while( i < n){
15             i++;
16             System.out.println("请输入第"+ i + "个学生的成绩:");
17             int score = sc.nextInt();
18             sum += score;
19         }
20         System.out.println("前"+ n + "个学生的总成绩为:" + sum + "
平均成绩为:" + (sum/n));
21     }
22 }
原文地址:https://www.cnblogs.com/YangGC/p/6058558.html