Java/과제

[Java] 메소드를 이용해서 세 과목을 입력받은 후 합계, 평균, 평균의 성취도, 취약 과목 출력하기 (기본편)

U__q 2022. 8. 28. 18:51
728x90

문제

세 과목의 점수를 입력받아 합계, 평균, 평균의 성취도, 취약과목을 출력하는 프로그램을 작성하라.

실행 결과 예시

점수를 입력하세요 : 50
점수를 입력하세요 : 80
점수를 입력하세요 : 90
====== 성적 처리 결과 ======
합계 : 220     평균 : 73
성취도 : C
====== 노력이 가장 필요한 교과 ======
과목 : 국어     점수 : 50

소스 코드

import java.util.Scanner;
public class Score123{
	public static void main(String args[]){
		Scanner sc=new Scanner(System.in);
		System.out.print("점수를 입력하세요 : "); // 세 과목 점수 입력 받음
		int score1=sc.nextInt();
		System.out.print("점수를 입력하세요 : ");
		int score2=sc.nextInt();
		System.out.print("점수를 입력하세요 : ");
		int score3=sc.nextInt();
		System.out.println("====== 성적 처리 결과 ======");

		int total=score1+score2+score3; // 세 과목 합계
		int ave=total/3; // 세 과목 평균
		System.out.println("합계 : "+total+"     평균 : "+ave);
		System.out.print("성취도 : ");
		if(ave>=90) //평균에 따른 성취도
			System.out.println("A");
		else if(ave>=80)
			System.out.println("B");
		else if(ave>=70)
			System.out.println("C");
		else
			System.out.println("D");

		System.out.println("====== 노력이 가장 필요한 교과 ======");
		if(score1<score2) { // 세 과목 점수를 비교해서 3등과목(취약과목) 출력
			if(score1<score3)
				System.out.println("과목 : 국어     점수 : "+score1);
			else
				System.out.println("과목 : 수학     점수 : "+score3); }
		else {
			if(score3>score2)
				System.out.println("과목 : 영어     점수 : "+score2);	
			else
				System.out.println("과목 : 수학     점수 : "+score3); }
	}	
}

소스 코드 풀이

		Scanner sc=new Scanner(System.in);
		System.out.print("점수를 입력하세요 : "); // 세 과목 점수 입력 받음
		int score1=sc.nextInt();
		System.out.print("점수를 입력하세요 : ");
		int score2=sc.nextInt();
		System.out.print("점수를 입력하세요 : ");
		int score3=sc.nextInt();
		System.out.println("====== 성적 처리 결과 ======");

Scanner 메소드를 사용해서 세 과목 점수를 입력받는다.

		int total=score1+score2+score3; // 세 과목 합계
		int ave=total/3; // 세 과목 평균
		System.out.println("합계 : "+total+"     평균 : "+ave);
		System.out.print("성취도 : ");
		if(ave>=90) //평균에 따른 성취도
			System.out.println("A");
		else if(ave>=80)
			System.out.println("B");
		else if(ave>=70)
			System.out.println("C");
		else
			System.out.println("D");

total은 세 과목의 합계, ave은 세 과목의 평균을 출력한다.

if문을 사용해서 평균에 따른 성취도를 구한다.

		System.out.println("====== 노력이 가장 필요한 교과 ======");
		if(score1<score2) { // 세 과목 점수를 비교해서 3등과목(취약과목) 출력
			if(score1<score3)
				System.out.println("과목 : 국어     점수 : "+score1);
			else
				System.out.println("과목 : 수학     점수 : "+score3); }
		else {
			if(score3>score2)
				System.out.println("과목 : 영어     점수 : "+score2);	
			else
				System.out.println("과목 : 수학     점수 : "+score3); }
	}	
}

if문을 사용해서 세 과목의 값을 비교하고, 3등과목(취약과목)을 출력한다.

실행 결과

점수를 입력하세요 : 80
점수를 입력하세요 : 90
점수를 입력하세요 : 75
====== 성적 처리 결과 ======
합계 : 245     평균 : 81
성취도 : B
====== 노력이 가장 필요한 교과 ======
과목 : 수학     점수 : 75

 

728x90