객체지향 자바 프로그래밍
4주
신림
입문
x 3
# 고득점자 찾기
가장 높은 성적을 받은 학생의 이름과 점수를 출력하시오.
### 성적표
```
Elena(65), Suzie(74), John(23), Emily(75), Neda(68),
Kate(96), Alex(88), Daniel(98), Hamilton(54)
```
### 예상출력
```
1등: Daniel(98)
```
### 스켈레톤 코드
```java
public class TopScoreStudent {
public static int topIndex(int[] scores) {
/* 가장 높은 성적의 인덱스를 반환하는 함수를 구현하세요. */
return 0;
}
public static void main(String[] args) {
String[] names = {/* 1. 이름을 배열로 만드세요. */};
int[] scores = {/* 2. 점수를 배열로 만드세요. */};
int i = topIndex(scores);
System.out.printf("1등: %s(%d)\n", names[i], scores[i]);
}
}
```
```
public class TopScoreStudent {
public static int topIndex(int[] scores) {
int TopVal = 0;
for(int i =0; i<scores.length;i++){
if(scores[TopVal] < scores[i]){
TopVal = i;
}
}
return TopVal;
}
public static void main(String[] args) {
String[] names = {"Elena","Suzie","John","Emily","Neda",
"Kate","Alex","Daniel","Hamilton"};
int[] scores = {65,74,23,75,68,96,88,98,54};
int i = topIndex(scores);
System.out.printf("1등: %s(%d)\n", names[i], scores[i]);
}
}
```
# 피드백 입니다
> 변수의 이름을 의미와 부합하게 지어주면 좋습니다.
topIndex() 메소드의 지역변수 **TopVal** 변수명을 **topIdx** 로 변경해주면 더 명확한 코드가 될 것 같아요. 추가로 자바에서 변수의 첫글자는 소문자고 해주는게 일반적인 명명법(Naming Rules)입니다.
```
int TopVal = 0;
```
to
```
int topIndex = 0;
```
```
package com.bdk.studyhomework;
public class TopScoreStudent {
public static int topIndex(int[] scores){
// 가장 큰 점수를 리턴하는 함수
int topScore = 0;
for(int x=0; x<9; x++){
if(topScore<scores[x]){
topScore = scores[x];
}
} return topScore;
}
public static void main(String[] args) {
String [] names = {"Elena" , "Suzie", "John" , "Emily" ,"Neda",
"Kate", "Alex", "Daniel", "Hamilton"};
int [] scores = {65, 74 , 23 , 75 , 68, 96, 88, 98, 54 };
int topScore =topIndex(scores);
System.out.println("가장 높은 점수는" +topScore+ "입니다");
}
}
```