gpt4 book ai didi

java - 线程ArrayIndex异常

转载 作者:行者123 更新时间:2023-12-01 15:03:21 25 4
gpt4 key购买 nike

我正在使用系列和输入进行一些初学者编程,但我不断遇到同样的问题。找不到解决方案。基本上我现在想要我的程序做什么,我输入一个数字列表并将它们打印出来。我得到了无论我在程序中更改什么,都会一遍又一遍地出现相同的错误。这是我的代码。

import java.util.Scanner;
public class Test437 {
public static void main(String[] args) {

int limit = 25;
int cnt;
int addtion;
double dbt; //Devided by two % 2

Scanner input = new Scanner(System.in);
int [] ya = new int[8];

for(cnt = 0;cnt < ya.length;cnt++)
{

System.out.print("ya[" + cnt + "]= ");
ya[cnt] = input.nextInt();

}

System.out.println(ya[cnt]);


}
}

我收到此错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 8 在 Test437.main(Test437.java:22)

最佳答案

System.out.println(ya[cnt]); 此行位于循环外。 Cnt 等于数组大小,因此不能以这种方式使用它,因为数组中没有具有该索引的元素。

关于java - 线程ArrayIndex异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13322637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com