영상
문제
package 배열2_문제;
/*
[문제] 1등학생의 학번과 성적 출력하시오.
[정답] 1004번(98점)
*/
public class 배열2_문제04_학생성적_문제 {
public static void main(String[] args) {
int[] numberList = {1001, 1002, 1003, 1004, 1005};
int[] scoreList = { 87, 11, 45, 98, 23};
}
}
Java
복사
해설
package 배열2_문제;
/*
[문제] 1등학생의 학번과 성적 출력하시오.
[정답] 1004번(98점)
*/
public class 배열2_문제04_학생성적_정답 {
public static void main(String[] args) {
int[] numberList = {1001, 1002, 1003, 1004, 1005};
int[] scoreList = { 87, 11, 45, 98, 23};
int maxScore = 0;
int maxIndex = 0;
/*
[풀이]
maxScore < scoreList[i]
i = 0 0 < 87 true maxScore = 87, maxIndex = 0
i = 1 87 < 11 false maxScore = 87, maxIndex = 0
i = 2 87 < 45 false maxScore = 87, maxIndex = 0
i = 3 87 < 98 true maxScore = 98, maxIndex = 3
i = 4 98 < 23 false maxScore = 98, maxIndex = 3
*/
for(int i=0; i<scoreList.length; i++) {
if(maxScore < scoreList[i]) {
maxScore = scoreList[i];
maxIndex = i;
}
}
System.out.println(numberList[maxIndex]);
System.out.println(maxScore);
}
}
Java
복사