我得到這個錯誤:檢查,如果兩組數字相等錯誤
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
這指的是這行代碼if(x[k]==y[j])
Scanner sc1 = new Scanner(System.in);
int [] x;
int [] y;
int size;
System.out.println("Numbers in launch code sequence should be entered on ");
System.out.println("single line, separated by blanks.");
System.out.println("");
System.out.println("Enter length of launch code sequence: ");
size = sc1.nextInt();
x = new int[size];
y = new int[size];
int k = 0;
int j = 0;
System.out.println("Mr. President, Enter the launch code sequence: ");
for(;k<x.length; k++){
x[k] = sc1.nextInt();}
System.out.println("Mr. Vice President, Enter the launch code sequence");
for(;j<y.length; j++){
y[j] = sc1.nextInt();
if(x[k]==y[j]){
System.out.println("All equal: Missile system cleared for launch.");
if(x[k]!=y[j]){
System.out.println("Codes do not check out. Abort missile launch.");
}
}
}
}
在行代碼 - >如果(X [k]的==ÿ [j]){ 你的k已經達到了x.length,所以它超出了界限。 – wxyz
適當的縮進有助於理解代碼的控制流程。如果你格式正確,你一定會發現你自己的錯誤。 – thSoft
我希望代碼不會因爲一些錯誤而啓動實際的導彈,並且會導致我們所知道的生命結束。那真的會毀了我的一天。 – NickJ