Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 22, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/734e1c607278bf768adf9bc816dd8a8b to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/734e1c607278bf768adf9bc816dd8a8b to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-19
import java.util.Scanner;
public class En06_19 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
// クラス数を決定
System.out.print("クラス数:");
int classMax = stdIn.nextInt();
int[][] classes = new int[classMax][];
System.out.println();
// 人数をクラス数分決定し、その生徒分点数を入力する
// 生徒全員分の変数(後に使用)
int student = 0;
for (int i = 0; i < classes.length; i++) {
System.out.printf("%d組の人数:", i + 1);
int studentMax = stdIn.nextInt();
student += studentMax;
classes[i] = new int[studentMax];
for (int j = 0; j < classes[i].length; j++) {
System.out.printf("%d組%d番の点数:", i + 1, j + 1);
classes[i][j] = stdIn.nextInt();
}
System.out.println();
}
System.out.println();
// 表示
int[] sums = new int[classes.length];
double[] avgs = new double[classes.length];
System.out.println(" 組 | 合計 平均 ");
System.out.println("---------------------");
for (int i = 0; i < classes.length; i++) {
for (int j = 0; j < classes[i].length; j++) {
sums[i] += classes[i][j];
avgs[i] = sums[i] / (double) classes[i].length;
}
System.out.printf("%2d組 | %7d %6.1f \n", i + 1, sums[i], avgs[i]);
}
// 合計を計算
int sum = 0;
for (Integer n : sums) {
sum += n;
}
// 平均を計算(合計÷全体)
// ※小数点の連続計算の結果を使用すると正しい計算ができない為
double avgSum = (double) sum / student;
System.out.println("---------------------");
System.out.printf(" 計 | %7d %6.1f ", sum, avgSum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment