자바 프로그래밍/코딩
ex09_2.java(성적분석 모의실험)
by 세인트킴
2023. 5. 6.
import java.util.Random;
public class ex09_3 {
public static void main(String[] args) {
Random rand = new Random();
int[] count = new int[11];
int sum = 0;
System.out.printf("score[]: \n");
for(int i=0; i<300; i++) {
int score = rand.nextInt(101);
System.out.print(" " + score);
if((i+1)%10 == 0) {
System.out.println();
}
sum += score;
count[score/10]++;
}
int totalCount = 0;
for(int i=0; i<=10; i++) {
totalCount += count[i];
}
double average = (double)sum/totalCount;
System.out.printf("\nAverage: %.2f\n", average);
for(int i=0; i<9; i++) {
System.out.printf("%3d - %3d: ", i*10,i*10+9);
for(int j=0; j<count[i]; j++) {
System.out.printf("*");
}
System.out.print(" "+count[i]);
System.out.println();
}
System.out.printf("%9d: ", 100);
for(int j=0; j<count[10]; j++) {
System.out.printf("*");
}
System.out.print(" "+count[10]);
System.out.println();
}
}