0

次のエラーが表示されます。

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.");
            }
        }
    }
}
4

4 に答える 4

0

この段階では

if(x[k]==y[j]){

k配列の反復処理が終了し、次の値に設定されますx.length

したがって範囲外

于 2013-10-04T08:13:26.477 に答える