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

在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 "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] + " ") 
}