平均
public class Main {
public static void main(String[] args) {
// ① 配列の準備: 5人分の点数を格納
int[] scores = {80, 75, 90, 65, 100};
// ② 合計点を入れる箱(変数)を0で初期化
int total = 0;
// ③ for文で、配列の全要素を順番に処理する
// scores.length は「配列の長さ(この場合 5)」を意味します
for (int i = 0; i < scores.length; i++) {
// i番目の点数(scores[i])を total に足していく
total = total + scores[i];
}
// ④ 合計点と平均点を計算・表示
int average = total / scores.length;
System.out.println("--------------------");
System.out.println("合計点: " + total);
System.out.println("平均点: " + average);
}
}
☆この前のコードとは少し違い、合格点と平均点のみでる。
テストの平均点と合計点を出したいときに便利!
好きに数字を変えてみよう




