영상
문제
package 배열1_개념;
/*
[문제]
10부터 50까지 array배열에 저장 후,
array 배열 안의 모든 값의 합을 출력하시오.
[정답]
150
*/
public class 배열1_개념03_기본문제_문제 {
public static void main(String[] args) {
int[] array = new int[5];
}
}
Java
복사
해설
package 배열1_개념;
/*
[문제]
10부터 50까지 array배열에 저장 후,
array 배열 안의 모든 값의 합을 출력하시오.
[정답]
150
*/
public class 배열1_개념03_기본문제_정답 {
public static void main(String[] args) {
int[] array = new int[5];
/*
[풀이]
total = total + arr[i]
i = 0 total = 0 + 10
i = 1 total = 10 + 20
i = 2 total = 30 + 30
i = 3 total = 60 + 40
i = 4 total = 100 + 50
i = 5 반복문 종료
*/
int total = 0;
for(int i=0; i<5; i++) {
array[i] = (i + 1) * 10;
// total = total + arr[i]
total += array[i];
}
System.out.println(total);
}
}
Java
복사