day13_SungjukEx02

육희영·2021년 10월 28일
0

기입한 학생 수의 성적 총점, 평균 구하기2

package com.java1.day13;

import java.util.Scanner;

public class SungjukEx02 {
	public static void main(String[] args) {	
		Input ip = new Input();
		ip.studentNum();
		ip.start();
	}
}

class Input {
	Scanner sc = new Scanner(System.in);
	int count;
	String name;
	int kor, eng, math;
	
	void studentNum() {
		System.out.print("학생의 수를 입력 : ");
		count = sc.nextInt();
	}
	
	void studentName() {
		System.out.print("학생의 이름 : ");
		name = sc.next();
	}
	
	void korScore() {
		System.out.print("국어 점수: ");
		kor = sc.nextInt();
	}
	
	void engScore() {
		System.out.print("영어 점수: ");
		eng = sc.nextInt();
	}
	
	void mathScore() {
		System.out.print("수학 점수: ");
		math = sc.nextInt();
	}
	
	void test() {
		System.out.print("학생의 이름,국어점수,영어점수,수학점수 : " );
		String str = sc.next();
		
		String[] arr = str.split(",");	//","를 기준으로 문자열을 잘라서 배열에 저장한다.
		
		name = arr[0];
		kor = Integer.parseInt(arr[1]);
		eng = Integer.parseInt(arr[2]);
		math = Integer.parseInt(arr[3]);
	}	
	
	void start() {
		Student1[] st = new Student1[count];
		for(int i=0; i<count; i++) {
			test();
			/*	
			studentName();
			korScore();
			engScore();
			mathScore();
			*/
			st[i] = new Student1(name, kor, eng, math);
			st[i].sum(kor, eng, math);
			st[i].average();
		}
		disp();
		for(int i=0; i<count; i++) {
			st[i].display();
		}
		sc.close();
	}
	
	void disp() {
		System.out.println("이름 \t 국어 \t 영어 \t 수학 \t 총점 \t 평균 "); 
		System.out.println("==================================================");
	}
}

class Student1 {
	String name;
	int kor, eng, mat;
	int total;
	float avg;
	
	Student1(String name, int kor, int eng, int mat){
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
	}
	
	int sum (int kor, int eng, int mat) {
		total = kor + eng + mat;
		return 	total;
	}
	
	float average () {
		avg = total/(float)3.0f;
		return avg;
	}
	
	void display() {
		System.out.printf("%s \t %3d \t %3d \t %3d \t %3d \t %.2f %n", 
				name, kor, eng, mat, total, avg);
	}
}

출력결과

0개의 댓글

관련 채용 정보