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]);
}
}
Code output value is
island = Fiji
island = Cozumel
island = Bermuda
island = Azores
It should come out like this, but I don't know where it went wrong.
I'm using Eclipse and there's a red underline on the ref.
java array
while(y<4) {
ref = index[y];
y=y+1;
System.out.print("island = ");
System.out.println(islands[ref]);
}
Try changing it like above. The output statement will be executed after the repeat statement while, so the result you want won't come out. To do as you please, you have to repeat the output in the repetitive sentence to get the result.
The reason why the ref is red is because it is not initialized, so it seems to have been processed in red.
© 2024 OneMinuteCode. All rights reserved.