如何解决线程中的异常;“主要”;java.lang.ArrayIndexOutOfBoundsException:3

如何解决线程中的异常;“主要”;java.lang.ArrayIndexOutOfBoundsException:3,java,exception,Java,Exception,我是一个新手,我不知道我的代码有什么问题。我将在这里写问题和代码。如果有人能帮我,请。 所以问题是: 你必须说出这个人在给定时间内能做的家务活的总数。 第一个输入是用户获得的总时间。 第二个输入是用户希望执行的家务总数。 最终输入是完成每个任务所需的时间 下面是我的代码: 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in) int totalmins,chores=0,eachtime,totalchores,counter=0; //获取输入 Sys

我是一个新手,我不知道我的代码有什么问题。我将在这里写问题和代码。如果有人能帮我,请。 所以问题是: 你必须说出这个人在给定时间内能做的家务活的总数。 第一个输入是用户获得的总时间。 第二个输入是用户希望执行的家务总数。 最终输入是完成每个任务所需的时间

下面是我的代码:

公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in)

int totalmins,chores=0,eachtime,totalchores,counter=0;
//获取输入
System.out.println(“输入总时间:”);
totalmins=scan.nextInt();
同时(总分钟数>100000){
System.out.println(“再次输入。小于100000:”);
totalmins=scan.nextInt();
}
System.out.println(“输入总家务:”);
chores=scan.nextInt();
int[]时间=新int[杂务];

对于(int i=1;i我认为您缺少的一点是,在长度为3的数组中,条目编号为0、1和2。如果您尝试使用条目编号3,您将得到该异常。但这正是您所做的-所有循环都将继续,直到
i=2
(包括这种情况),但随后您继续尝试使用数组的条目
i+1

如果必须猜测,您会说错误是什么(给定
java.lang.ArrayIndexOutOfBoundsException
)。我认为我的数组for循环计数器出了问题?在for循环中尝试-1。或者吃一个面包圈。逐步遍历for循环。
I
的最后一个值是什么?与数组大小相关的
I+1
的值是什么?我尝试了time.length-1,但我的程序甚至不会通过第一个for循环ows循环中第二个输入的错误
    int totalmins, chores=0, eachtime, totalchores, counter=0;


    // getting the input
    System.out.println("Enter the total time:");
    totalmins=scan.nextInt();
    while (totalmins>100000) {
        System.out.println("Enter again. Less than 100000:");
        totalmins=scan.nextInt();
    }

    System.out.println("Enter the total chores:");
    chores=scan.nextInt();

    int [] time = new int[chores];

    for (int i=1; i<chores; i++) {
        System.out.println("Enter time:");
        eachtime=scan.nextInt();
        time[i]=eachtime;
    }
   // arranging in ascending order
    for (int i=0;i<time.length; i++) {
        if (time[i] > time[i+1]) {
            int temp = time[i];
            time[i]=time[i+1];
            time[i+1]=temp;
        }

        }
        for (int i=0;i<time.length; i++) {
            totalchores=time[i] + time[i+1];
            counter++;

            if (totalchores>totalmins) {
                counter=counter-1;
                System.out.println(counter);
            }
        }
    }