자바에서 array 공부 중입니다. 출력이 안되어서 질문드립니다.
조회수 875회
public class TestArrays {
public static void main(String[]args) {
int [] index = new int[4];
index[0]=1;
index[1]=3;
index[2]=0;
index[3]=2;
String[] islands = new String[4];
islands[0]="Bermuda";
islands[1]="Fiji";
islands[2]="Azores";
islands[3]="Cozumel";
int y = 0;
int ref;
while(y<4) {
ref = index[y];
y=y+1;
}
System.out.print("island = ");
System.out.println(islands[ref]);
}
}
코드 출력값은
island = Fiji
island = Cozumel
island = Bermuda
island = Azores
이렇게 나와야하는데 어디서 어디가 잘못됐는지 모르겠습니다..
이클립스로 이용중인데 ref에 빨간 밑줄이 그여있습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
while(y<4) { ref = index[y]; y=y+1; System.out.print("island = "); System.out.println(islands[ref]); }
위와 같이 바꿔서 해보세요. 지금 출력문이 반복문 while을 다 끝나고 실행되기 때문에 원하시는 결과가 안 나올거에요. 원하시는 대로 하려면 출력을 반복문안에서 반복해야 결과를 얻을 실거에요.
ref가 빨간줄 뜨는 이유는 초기화가 안되어 있는 상태여서 빨간색으로 처리된거 같네요.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력