본문 바로가기

java

java -성적 입출력 배열 담기

main

package default1;


public class Calc {

	private String name; // 이름

	private int kor; // 국어

	private int eng; // 영어

	private int math; // 수학

	private int[] score; // 총점

	private float avg; // 평균
	
	public Calc() {
		score = new int[4];
	}

	public Calc(String name, int[] score, float avg) { // 생성자
		// TODO Auto-generated constructor stub
			
		this.name = name;

		this.score = score;

		this.avg = avg;

	}

	public String getName() { // 이름 입력
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getKor() { // 국어 입력
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() { // 영어 입력
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMath() { // 수학 입력
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}
	

	public int total(int sum) {		//총점 계산
		sum = getKor()+getEng()+getMath();
		
		return sum;
	}
	
	public float avg(float num) {
	
		int sum = 0;
		return num = total(sum)/3;
	
	}
	
	public void setScore(int i, int score) {

		
		this.score[i] = score;
				
		
	}

	public void setScore(int[] score) {

		this.score = score;

	}

	public int getScore(int i) {
		
		
		
		return score[i];

	}

	public int[] getScore() {

		return score;

	}
	

}

 

method

 

package default1;

import java.util.Scanner;



public class ScoreProgram {

	public static void main(String[] args) {
		
		
		
		Scanner sc =new Scanner(System.in);
		
		Calc calc = new Calc();
		
		
		int sum = 0;	//총점 을 가져온다.
		
		int i = 0;   //인덱스 를 가져오기위해 쓰는 변수
		
		System.out.println("이름을 입력해주세요");
		calc.setName(sc.next());
		
		System.out.println("국어 점수를 입력해주세요");
		calc.setKor(sc.nextInt());
		
		System.out.println("영어 점수를 입력해주세요");
		calc.setEng(sc.nextInt());
		
		System.out.println("수학 점수를 입력해주세요");
		calc.setMath(sc.nextInt());
		
		
		calc.setScore(0 ,calc.getKor());		//setScore 국어 영어 수학 총점 저장
		calc.setScore(1 ,calc.getEng());
		calc.setScore(2 ,calc.getMath());
		calc.setScore(3 ,calc.total(sum));
		
			float num = 0;		//avg 값을 출력한다.
			
			
			System.out.println("===================================================");
			
		
			
			System.out.println(" 국어점수  ; 영어 점수 ; 수학점수 ; 총점;");
			for(int j = 0 ; j < 4; j++) {
					
				System.out.print(calc.getScore(j)+"\t");	//저장한 스코어값을 인덱스 순서로 출력
			}
			System.out.println();
			
			
			
			System.out.println(calc.getName()+"\t"+calc.getKor()+"\t"+calc.getEng()+"\t"+calc.getMath()+"\t"+calc.total(sum)+"\t"+calc.avg(num));
			
			

	}
}

 

 

출력문

 

이름을 입력해주세요
리동동
국어 점수를 입력해주세요
100
영어 점수를 입력해주세요
90
수학 점수를 입력해주세요
80
===================================================
 국어점수  ; 영어 점수 ; 수학점수 ; 총점;
        100         90          80       270
리동동 100 90 80 270 90.0