这从开始到第一个循环部分运行顺利,但是当显示用户输入时(在第二个循环上),程序停止并且在 cmd 中有一条消息:
(Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at
CaseStudy2A.main(CaseStudy2A.java:17)
代码:
import java.util.Scanner;
public class CaseStudy2A {
public static void main(String[] args) {
Scanner inp = new Scanner (System.in);
int inpNum;
System.out.print("Enter Number: ");
inpNum = inp.nextInt();
int num[]=new int [inpNum];
int accu;
for(int x=1;x<=inpNum;x++) {
System.out.print("\nNumber [" + x + "] : ");
accu = inp.nextInt();
num[x]=accu;
}
for(int x=1;x<=inpNum;x++)
System.out.println(num[x] + " ");
}
}