JAVA ArrayList 연습문제

어뮤즈온·2020년 12월 14일
0

초급자바

목록 보기
29/31

25명 학생의 국어, 영어, 수학, 사회, 과학, Orcle, Java 점수를 0~ 100까지 랜덤으로 생성하고, 아래와 같이 출력하시오.

ArrayList<String> students = new ArrayList<>();

students.add("학생1");
students.add("학생2");
students.add("학생3");
students.add("학생4");
students.add("학생5");
students.add("학생6");
students.add("학생7");
students.add("학생8");
students.add("학생9");
students.add("학생10");
students.add("학생11");
students.add("학생12");
students.add("학생13");
students.add("학생14");
students.add("학생15");
students.add("학생16");
students.add("학생17");
students.add("학생18");
students.add("학생19");
students.add("학생20");
students.add("학생21");
students.add("학생22");
students.add("학생23");
students.add("학생24");
students.add("학생25");

ArrayList<String> subjects = new ArrayList<>();

subjects.add("국어");
subjects.add("영어");
subjects.add("수학");
subjects.add("사회");
subjects.add("과학");
subjects.add("Oracle");
subjects.add("Java");

//점수발생
ArrayList<ArrayList<Integer>> scores = new ArrayList<>();

for(int i = 0; i < scores.size(); i++){
	ArrayList<Integer> score = new ArrayList<>();
	for(int j = 0; j < subjects.size(); j++){
		score.add((int)(Math.random() * 101));
	}
	scores.add(score);
}

//합계 / 평균
ArrayList<Integer> sums = new ArrayList<>();
ArrayList<Double> avgs = new ArrayList<>();

for(int i = 0; i < scores.size(); i++){
	int sum = 0;
	for(int j = 0; j < subjects.size(); j++){
		sum += scores.get(i).get(j);
	}
	double avg = Math.random((double) sum / subjects.size() * 100) / 100.0;
	sums.add(sum);
	avgs.add(avg);
}

//석차구하기
ArrayList<Integer> ranks = new ArrayList<>();

for(int i = 0; i < sums.size(); i++){
	ranks.add(1);
	for(int j = 0; j < sums.size(); j++){
		if(sums.get(i) < sums.get(j)){
			ranks.set(i, ranks.get(i) + 1);
		}
	}
}

//정렬
for(int i = 0; i < scores.size()-1 i++){
	int min = i;
	for(int j = i+1; j < scores.size(); j++){
		if(ranks.get(j) < ranks.get(min)){
			min = j;
		}
	}
	String name = stdents.get(i);
	student.set(i, students.get(min));
	student.set(min, name);

	ArrayList<Integer> list = scores.get(i);
	scores.set(i, scores.get(min));
	scores.set(min, list);

	Integer sum = sums.get(i);
	sums.set(i, sums.get(min));
	sums.set(min, sum);

	Double avg = avgs.get(i);
	avgs.set(i, avgs.get(min));
	avgs.set(min, avg);

	int rank = ranks.get(i);
	ranks.set(i, ranks.get(min));
	ranks.set(min, rank);
}

//출력
for(int i = 0; i < subjects.size(); i++){
	System.out.println("\t" + subjects.get(i));
}
System.out.println("\t합계\t평균\t석차");

for(int i = 0; i < students.size(); i++){
	System.out.print(students.get(i) + "\t");
	for(int j = 0; j < subjects.size(); j++){
		System.out.print(scores.get(i).get(j) + "\t");
	}
	System.out.print(sums.get(i) + "\t");
	System.out.print(avgs.get(i) + "\t");
	System.out.print(ranks.get(i) + "\t");
	System.out.println();
}

//과목합계, 과목평균 출력
ArrayList<Integer> subjectSums = new ArrayList<>();
ArrayList<Double> subjectAvgs = new ArrayList<>();
for(int i = 0; i < subjects.size(); i++){
	int sum = 0;
	for(int j = 0; j < students.size(); j++){
		sum += scores.get(j).get(i);
	}
	double avg = Math.round((double)sum / students.size() * 100) / 100.0;
	subjectSums.add(sum);
	subjectAvgs.add(avg);
}
		
System.out.print("과목합계");
for(int i = 0; i < subjectSums.size(); i++){
	System.out.print("\t" + subjectSums.get(i));
}
System.out.println();
System.out.print("과목평균");
for(int i = 0; i < subjectAvgs.size(); i++){
	System.out.print("\t" + subjectAvgs.get(i));
}
System.out.println();
profile
Hello, world!

0개의 댓글