Skip to content

Instantly share code, notes, and snippets.

@nesffer
Last active November 16, 2015 13:30
Show Gist options
  • Save nesffer/01c9d37f7e587b5983b3 to your computer and use it in GitHub Desktop.
Save nesffer/01c9d37f7e587b5983b3 to your computer and use it in GitHub Desktop.
Java 성적처리
import java.util.Random;
import java.util.stream.IntStream;
public class d2015_11_16_1 {
public static void main(String[] args) {
Random ran = new Random();
String[] name = {"신지민", "김재권", "송영교", "김명중", "이기현"};
int[][] num = new int[5][5];
int[] sum_row = new int[5];
int[] sum_col = {0, 0, 0, 0, 0};
double[] avg = new double[5];
double sum_avg = 0;
int[] rank = {1, 1, 1, 1, 1};
// 난수 초기화
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length; j++)
num[i][j] = ran.nextInt(100);
// 행의 합
sum_row[i] = IntStream.of(num[i]).sum();
avg[i] = sum_row[i] / sum_row.length;
}
// 열의 합
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++)
sum_col[i] += num[j][i];
}
// 순위
for (int i = 0; i < rank.length; i++) {
for (int j = 0; j < rank.length; j++) {
if (avg[i] < avg[j])
rank[i] += 1;
}
}
System.out.println("| 이름 | 국어 | 영어 | 수학 | 사회 | 과학 | 총점 | 평균 | 석차 |");
System.out.println("|--------+------+------+------+------+------+-------+-------+------|");
// 이름, 행, 행의 합, 열의 합, 순위 출력
for (int i = 0; i < num.length; i++) {
System.out.printf("| %s |", name[i]);
for (int aNum : num[i]) {
System.out.printf(" %4d |", aNum);
}
System.out.printf(" %5d |", sum_row[i]);
System.out.printf(" %5.1f |", avg[i]);
System.out.println(String.format(" %2d |", rank[i]));
}
System.out.println("|--------+------+------+------+------+------+-------+-------+------|");
System.out.print("| 총합 |");
for (int col : sum_col)
System.out.printf(" %4d |", col);
System.out.printf(" %5d |", IntStream.of(sum_row).sum());
for (double a : avg)
sum_avg += a;
System.out.printf(" %4.1f |", sum_avg);
System.out.println(" |");
System.out.print("| 평균 |");
for (int col : sum_col)
System.out.printf(" %4.1f |", col / (float) name.length);
System.out.printf(" %5.1f |", IntStream.of(sum_row).sum() / (float) name.length);
System.out.printf(" %5.1f |", sum_avg / (float) name.length);
System.out.println(" |");
}
}
| 이름 | 국어 | 영어 | 수학 | 사회 | 과학 | 총점 | 평균 | 석차 |
|--------+------+------+------+------+------+-------+-------+------|
| 신지민 | 92 | 51 | 80 | 97 | 38 | 358 | 71.0 | 1 |
| 김재권 | 74 | 4 | 86 | 58 | 56 | 278 | 55.0 | 4 |
| 송영교 | 33 | 13 | 92 | 69 | 81 | 288 | 57.0 | 3 |
| 김명중 | 52 | 33 | 96 | 10 | 83 | 274 | 54.0 | 5 |
| 이기현 | 16 | 54 | 74 | 91 | 62 | 297 | 59.0 | 2 |
|--------+------+------+------+------+------+-------+-------+------|
| 총합 | 267 | 155 | 428 | 325 | 320 | 1495 | 296.0 | |
| 평균 | 53.4 | 31.0 | 85.6 | 65.0 | 64.0 | 299.0 | 59.2 | |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment