我收到錯誤消息後,我進入退出它說數組索引越界的異常:2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
林可怕在編程所以請與我裸露
import java.util.*;
public class Highway{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Total number of the exits");
int p=input.nextInt();
System.out.println("The first exit");
int i=input.nextInt();
System.out.println("The second exit");
int j=input.nextInt();
int[]A= new int[p];
for(int w=0;w<=p;i++) {
A[i]=(int)(java.lang.Math.random() * 20 + 1);
System.out.println(A[w]);
}
}
static void Distance(int i, int j, int[] A) {
// a is the array of distance
// this find the distances between i and j
int distance = 0;
if(j>i){
for(int k = i; k<=j;k++) {
distance=distance+A[k];
}
distance=distance-A[i];
}
if(i>j){
for (int m = j; m<=i; m++){distance=distance+A[m];
}
distance=distance-A[j];
}
System.out.println("The distance of the first"+i+" and second exit"+j+" is"+distance);
}
}
這裏是堆棧跟蹤? –
另外,你有沒有嘗試單步執行代碼來查看哪一行會引發異常? – KornMuffin