for (int y = 0; y <= arr.length;y++) { if中的第二个条件 y<=arr.length-1 你是y<=arr.length 数组是通过下标获取元素的,下标是从0开始,那么如果new了一个长度为5的数组,当Y=5的时候 你获取的是arr【5】,获取的就是第6个元素,那不就下标越界了吗。不知道这个是不是你的问题;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int i = s.nextInt();
int[] arr = new int[i];
for (int x = 0; x <=arr.length - 1; x++) {
arr[x] = s.nextInt();
}
for(int o = 0; o<arr.length;o++)
for (int y = 0; y <= arr.length;y++) {
if (arr[o] + arr[y] < 0) {
System.out.println("icebound is sad");
} else if (arr[o] + arr[y] > 0) {
System.out.println("icebound is happy");
} else {
System.out.println("icebound is ok.");
}
}
}
}[/quote]这个是我写的代码
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int i = s.nextInt();
int[] arr = new int[i];
for (int x = 0; x <=arr.length - 1; x++) {
arr[x] = s.nextInt();
}
for(int o = 0; o<arr.length;o++)
for (int y = 0; y <= arr.length;y++) {
if (arr[o] + arr[y] < 0) {
System.out.println("icebound is sad");
} else if (arr[o] + arr[y] > 0) {
System.out.println("icebound is happy");
} else {
System.out.println("icebound is ok.");
}
}
}
}
public static void main(String[] args) { Scanner s = new Scanner(System.in); int i = s.nextInt(); int[] arr = new int[i]; for (int x = 0; x <=arr.length - 1; x++) { arr[x] = s.nextInt(); } for(int o = 0; o<arr.length;o++) for (int y = 0; y <= arr.length;y++) { if (arr[o] + arr[y] < 0) { System.out.println("icebound is sad"); } else if (arr[o] + arr[y] > 0) { System.out.println("icebound is happy"); } else { System.out.println("icebound is ok."); } } }}