public class test {
public static void main(String[] args){
for (int i = 0; i < 2; i++) {
Scanner in=new Scanner(System.in);
System.out.println("請輸入成績");
int[] a=new int[i];
a[i]=in.nextInt();
System.out.println("成績是"+a[i]);
}}}
上面這種寫法報數組下標超限的錯。。。
public class BlackDemo {
static int[] b;
public static void main(String[] args){
for (int i = 0; i < 2; i++) {
Scanner in=new Scanner(System.in);
System.out.println("請輸入成績");
b[i]=in.nextInt();
System.out.println("成績是"+b[i]);
}
}
}
這個報空指針。。。難道靜態的全局變量,在for循環中被復制了還不能更改?
int[] a=new int[i];
a[i]=in.nextInt();
i初始值是0,數組長度是0,a[0]就會數組越界啊