성적관리 프로그램을 만들어주세요.
public class Student {
int kor;
int eng;
int math;
int sum;
double avg;
String name;
int rank;
}
public static void main(String[] args) {
Student[] students = new Student[10]; //학생 10명의 정보 저장할수 있는 배열 생성- Student 참조형타입
for(int i = 0; i < students.length; i++){
Student student = new Student(); //객체 생성
//위 식이 for문 밖에 있으면, 학생을 1명 만들고 정보를 10번 바꾸는것
student.name = "학생" + (i + 1);
student.rank = 1;
student.kor = (int)(Math.random() * 101);
student.eng = (int)(Math.random() * 101);
student.math = (int)(Math.random() * 101);
students[i] = student;
}
//for문이 끝나면 10명의 학생의 정보가 students 배열에 저장되어 있다.
//점수 합계, 평균
for(int i = 0; i < students.length; i++){
students[i].sum = students[i].kor + students[i].eng + students[i].math;
students[i].avg = Math.round((double)students[i].sum / 3.0 * 100) / 100.0;
}
//석차
for(int i = 0; i < students.length; i++){
for(int j = 0; j < students.length; j++){
if(students[i].sum < students[j].sum){
students[i].rank++;
}
}
}
//정렬
for(int i = 0; i < students.length-1; i++){
int max = i;
for(int j = i+1; j < students.length; j++){
if(students[max].sum < students[j].sum){
max = j;
}
}
Student temp = students[i];
students[i] = students[max];
students[max] = temp;
}
//출력
System.out.println("\t국어\t영어\t수학\t합계\t평균\t석차");
for(int i = 0; i < students.length; i++){
System.out.println(students[i].name + "\t" + students[i].kor + "\t" + students[i].eng + "\t" + students[i].math + "\t" + students[i].sum + "\t" + students[i].avg + "\t" + students[i].rank);
}
//과목합계, 과목평균 출력
int kor_sum = 0;
int eng_sum = 0;
int math_sum = 0;
for(int i = 0; i < 3; i++){
kor_sum += students[i].kor;
eng_sum += students[i].eng;
math_sum += students[i].math;
}
double kor_avg = Math.round((double)kor_sum / students.length * 100) / 100.0;
double eng_avg = Math.round((double)eng_sum / students.length * 100) / 100.0;
double math_avg = Math.round((double)math_sum / students.length * 100) / 100.0;
System.out.println("과목합계\t" + kor_sum + "\t" + eng_sum + "\t" + math_sum);
System.out.println("과목평균\t" + kor_avg + "\t" + eng_avg + "\t" + math_avg);
}