기본숙제(필수): p.142 직접 해보는 손코딩(switch문) 실행 후 결과 화면 캡쳐하기
추가 숙제(선택): p.202 05-2 확인 문제 6번 풀고 정리하기
package sec02.verify.exam06;
import java.util.Scanner;
public class Exam06 {
public static void main(String[] args) {
boolean run = true;
int studentNum = 0;
int[] scores = null;
Scanner scanner = new Scanner(System.in);
while(run) {
System.out.println("---------------------------------------------");
System.out.println("1.학생수 | 2.점수입력 | 3.점수리스트 | 4.분석 | 5.종료");
System.out.println("---------------------------------------------");
System.out.print("선택> ");
int selectNo = Integer.parseInt(scanner.nextLine());
if(selectNo == 1) {
System.out.print("학생수> ");
studentNum = Integer.parseInt(scanner.nextLine());
scores = new int[studentNum];
} else if(selectNo == 2) {
if(checkStudentNum(studentNum)) continue;
for (int i = 0; i < scores.length; i++) {
System.out.print("scores[" + i + "]> ");
scores[i] = Integer.parseInt(scanner.nextLine());
}
} else if(selectNo == 3) {
if(checkStudentNum(studentNum)) continue;
for (int i = 0; i < scores.length; i++) {
System.out.println("scores[" + i + "]: " + scores[i]);
}
} else if(selectNo == 4) {
if(checkStudentNum(studentNum)) continue;
int max = 0;
int sum = 0;
for (int i = 0; i < scores.length; i++) {
max = (max < scores[i]) ? scores[i] : max;
sum += scores[i];
}
System.out.println("최고 점수: " + max);
System.out.println("평균 점수: " + sum / studentNum);
} else if(selectNo == 5) {
run = false;
}
}
System.out.println("프로그램 종료");
}
public static boolean checkStudentNum(int studentNum) {
if(studentNum == 0) {
System.out.println("학생수를 입력하세요.");
return true;
}
return false;
}
}