package practise;
import java.util.Scanner;
public class no2_2
{
public static void main(String[] args)
{
int[] a = null;
int[] b = null;
boolean k = true;
int i = 0;
System.out.println("请输入数组:");
//Scanner input = new Scanner(System.in);
while(k)
{
Scanner input = new Scanner(System.in);
if(input.hasNextInt())
{
a[i] = input.nextInt(); //报错位置
i++;
}
else
break;
}
for(i = 0; i < a.length; i++)
{
b[i]=a[i]*a[i];
System.out.print(b[i] + " ");
}
}
}
int[] a = null; 你这里只是声明了数组,没有给空间,后面加上----a[]=new int[10];
或者直接在声明的时候------int[] a=new int[10];
你没有给数组分配空间,所以会报错
int[] a = new int[100];
int[] b = new int[100];
//分配内存就好
数组new出来后 它的长度就已经确定 不不给他一定的size是不行的
赋值时会报空指针 因为 数组时固定长度的 在数组的动态初始化时 没有给数组定义长度 所以不能给数组赋值
数组a,b的长度都没定义