嘿傢伙我得到這個錯誤我做了同樣的C和它的工作,但是當我在java中做它我得到錯誤「線程中的異常」主要「Java .lang.ArrayIndexOutOfBoundsException:5" 你可以看看這個 - >在java中的數組越界,但它在C工作
import java.util.Scanner;
class sort {
public static void main(String args[]) {
Scanner obj = new Scanner(System.in);
int a[] = new int[5];
int i, j;
int temp;
System.out.println("Enter the elements of array : ");
for (i = 0; i < 5; i++) {
a[i] = obj.nextInt();
}
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++) {
if (a[i + 1] < a[i]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
for (i = 0; i < 5; i++)
System.out.println("\n" + a[i]);
}
}
可能因爲C不檢查數組邊界? https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – jackarms
在這段代碼中if(a [i + 1] < a [i])'if'i == 5' then OOB –
我沒有理由懷疑跨語言的東西。 – ChiefTwoPencils