while 문과 Scanner의 nextLine() 메소드를 이용한 간단한 점수 분석 프로그램
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
boolean run = true;
int students = 0;
int[] scores = null;
while (run) {
System.out.print("""
------------------------------------------------------
1. 학생수 | 2. 점수입력 | 3. 점수리스트 | 4. 분석 | 5. 종료
------------------------------------------------------
선택 > """);
int inputInt = sc.nextInt();
if (inputInt == 1) {
System.out.print("학생 수 > ");
students = sc.nextInt();
scores = new int[students];
} else if (inputInt == 2) {
for (int i = 0; i < students; i++) {
System.out.printf("scores[%d] > ", i);
scores[i] = sc.nextInt();
}
} else if (inputInt == 3) {
for (int i = 0; i < students; i++) {
System.out.printf("scores[%d]: ", i);
System.out.println(scores[i]);
}
} else if (inputInt == 4) {
int best = 0;
float sumScores = 0;
for (int score : scores) {
sumScores += score;
if (score > best) {
best = score;
}
}
System.out.printf("최고 점수: %d\n", best);
System.out.printf("평균 점수: %.1f\n", sumScores / students);
} else if (inputInt == 5) {
System.out.println("프로그램 종료");
run = false;
}
}