기입한 학생 수의 성적 총점, 평균 구하기1
package com.java1.day13;
import java.util.Scanner;
public class SungjukEx01 {
public static void main(String[] args) {
int kor = 0;
int eng = 0;
int mat = 0;
Scanner sc = new Scanner(System.in);
System.out.printf("학생의 수를 입력 : ");
int count = sc.nextInt();
Student[] st = new Student[count];
for(int i=0; i<count; i++) {
System.out.print("학생의 이름 : ");
String name = sc.next();
System.out.print("국어 점수: ");
kor = sc.nextInt();
System.out.print("영어 점수: ");
eng = sc.nextInt();
System.out.print("수학 점수 : ");
mat = sc.nextInt();
st[i] = new Student(name, kor, eng, mat);
st[i].sum(kor, eng, mat);
st[i].average();
}
disp();
for(int i=0; i<count; i++) {
st[i].display();
}
sc.close();
}
static void disp() {
System.out.println("이름 \t 국어 \t 영어 \t 수학 \t 총점 \t 평균 ");
System.out.println("==================================================");
}
}
class Student {
String name;
int kor, eng, mat;
int total;
float avg;
Student(String name, int kor, int eng, int mat){
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
sum(this.kor, this.eng, this.mat);
}
void sum (int kor, int eng, int mat) {
total = kor + eng + mat;
}
void average () {
avg = total/(float)3.0f;
}
void display() {
System.out.printf("%s \t %3d \t %3d \t %3d \t %3d \t %.2f %n", name, kor, eng, mat, total, avg);
}
}
출력결과
