简体   繁体   English

当我运行我的代码时,出现一个错误,说“线程中的异常”主”java.lang.ArrayIndexOutOfBoundsException:-1”

[英]An error saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1" shows up when I run my code

Here is my code:这是我的代码:

    Scanner input= new Scanner (System.in);
    
    int[][] anArray= new int[2][2];
    int[][] anArray1= new int[2][2];
    int[][] anArray2= new int[2][2];
    int temp1=1, temp2=1;
    
    for(int a=0; a<anArray.length; a++){
        for(int b=0; b<anArray.length; b++){
            System.out.println("Enter value in anArray index["+a+"]["+b+"]:");
            anArray[a][b]= input.nextInt();
            
            System.out.println("Enter value in anArray1 index["+a+"]["+b+"]:");
            anArray[a][b]= input.nextInt();
        }
    for(int c=0; c<anArray2.length; c++){
        for(int d=0; d<anArray2.length; d++){
            anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];
            System.out.println("Total of "+ anArray[c][d]+ " and "+ anArray1[temp1][temp2]+ " is " + anArray2[c][d]);
        temp2--;
    }
        temp1--;
        temp2=1;
    }
    }

This is a laboratory from my class.这是我的 class 的实验室。 And once I simulated it, I don't quite get these parts of the code where it involved temp1 and temp2一旦我模拟了它,我就不太明白涉及 temp1 和 temp2 的这些代码部分

    int temp1=1, temp2=1;

    anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];

The line of error is here:错误行在这里:

    anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];

Please helpppp请帮助ppp

In this line:在这一行:

System.out.println("Enter value in anArray1 index["+a+"]["+b+"]:"); anArray[a][b]= input.nextInt();

You typed anArray[a][b] = input.nextInt();你输入anArray[a][b] = input.nextInt(); instead of anArray1[a][b] = input.nextInt();而不是anArray1[a][b] = input.nextInt();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译程序时出现错误(线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException) - I get the Error when i compile my program (Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException) 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:3当我在预设集合中更改no时,它给出了错误 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 When i change no in my predermined set it give error 代码错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:1 - Error in code: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1 当我尝试运行此代码时,它将导致“线程“ main”中的异常“ java.lang.ArrayIndexOutOfBoundsException:0” - When i tried running this code it causes “ Exception in thread ”main“ java.lang.ArrayIndexOutOfBoundsException: 0” this exception 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:Java 中的 0 错误 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java “线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:0” java错误 - “Exception in thread ”main“ java.lang.ArrayIndexOutOfBoundsException: 0” java error 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:Java中出现0错误 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java Java错误“线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException” - Java Error “Exception in thread ”main“ java.lang.ArrayIndexOutOfBoundsException” Java错误:线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException - Java error: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM