자바실전_Day_11_01_배열(응용)
-----------------------------------------------------------------------------------
1번째 예제
//#1. 변수선언(배열)
//(방법.1)
int[] score = new int[5];
score[0] = 100;
score[1] = 88;
//(방법.2)
int[] score2 = new int[] {100, 88, 100, 90, 50};
//(방법.3)
int[] score3 = {100, 88, 100,90, 50};
String[] studentName = {"홍길동", "김자바", "이자바", "박디비", "송디비"};
int count =0;//인원수체크(배열 길이로 인원수를 사용해도 됨)
int sum = 0; //합계
double avg = 0.0; //float avg = 0.0f; 평균
//지역변수는 초기화를 시킨다.
//float avg = 0.0f; 이렇게 붙여서 float과 double을 구분한다.
//(방법.2) 계산 작업(합계, 평균, 인원수)
for(int i=0; i<score.length; i++) {
count++; //인원수, count = count + 1; count += 1;
sum = sum + score3[i]; // sum +=score[i];
}
// ===============================
// 번호 이름 성적
// ===============================
// 1 홍길동 100
// 2 김자바 88
// ....
// ===============================
// 인원수 : 5명
// 전체 합계 : ??
// 전체 평균 : ??.??
//평균
avg = (double)sum / score3.length;
//#3. 출력
System.out.println("===============================");
System.out.println("번호\t이름\t 성적");
System.out.println("===============================");
for(int i=0; i <score.length; i++) {
System.out.print(" "+(i+1)+"\t");
System.out.print(studentName[i]+"\t ");
System.out.println(score3[i]);
}
System.out.println("===============================");
System.out.println("전체 합계 = " + sum);
System.out.println("전체 평균 = " + avg);
}