Java 带数组的字符串输入
在Java中,数组的索引从Java 带数组的字符串输入,java,arrays,string,Java,Arrays,String,在Java中,数组的索引从0到length-1,而不是从1到length。数组是基于0的。你循环了一个太高的值,并且你的数组已经用完了 改变 Please enter the Number of Students you would like to input for 2 Please input the students names What is there score? 5 Please input the students names Exception in thread "ma
0
到length-1
,而不是从1
到length
。数组是基于0的。你循环了一个太高的值,并且你的数组已经用完了
改变
Please enter the Number of Students you would like to input for
2
Please input the students names
What is there score?
5
Please input the students names
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at ProgramAssignment1.reader(ProgramAssignment1.java:18)
at ProgramAssignment1.main(ProgramAssignment1.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
for(int i=1;i1.您正在迭代循环到10
for(int a=1;a您的问题是索引从0开始,而不是从1开始。
更改:
for(int i=1;i数组从索引0开始。尝试迭代从0开始的bucles。Java中的数组,就像大多数编程语言一样数组
用偏移量来寻址
这意味着name[1]
指向超过名称数组开头的对象1,因为偏移量为1
相反,数组的第一个元素将0超过数组的开始部分
或name[0]
,最后一个元素将数组的长度减去数组开始部分的1
或name[name.length-1]
在(int i=1;iZouZou)的代码中,ZouZou是正确的。数组是基于0的,您需要运行从0到array.length-1的循环
例如,如果我有:
for (int i = 0; i <= count;i++) {
System.out.println("Please input the students names ");
name[i] = input.nextLine();
System.out.println("What is there score?");
scores[i] = input.nextInt();
}
int[]数组={1,2,3,4,5,6,7,8,9,10}
对于(int i=0;i数组是0基索引的。也就是说,索引从0开始到array.length-1。请阅读以下内容:ok,现在不允许我输入字符串使用input.nextLine()
在input.nextLine()之后使用
for (int i = 1; i <= count;i++) {
for (int i = 0; i < count;i++) {
`for (int i = 1; i <= count;i++)`
for (int i = 0; i < count;i++)
for (int i = 1; i <= count;i++) {
System.out.println("Please input the students names ");
name[i] = input.nextLine();
System.out.println("What is there score?");
scores[i] = input.nextInt();
}
for (int i = 0; i <= count;i++) {
System.out.println("Please input the students names ");
name[i] = input.nextLine();
System.out.println("What is there score?");
scores[i] = input.nextInt();
}
int[] array = {1,2,3,4,5,6,7,8,9,10}
for(int i=0; i<array.length; i++ {
System.out.println(array[i] + " ")
}