- Java code
public static void main(String[] args){ Scanner sc=new Scanner(System.in); String [][] a=new String [2][]; String [] b=null; for(int i=0;i<a.length;i++) { b=sc.nextLine().split(" "); for(int j=0;j<b.length;j++) { a[i]=b; }} for(int i=0;i<a.length;i++){ for(int j=0;j<b.length;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } }
输入格式为:
i like china
i want to stay in there
错误为:
i like china Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at exam2.main(exam2.java:17)
输入格式为:
i want to stay in there
i like china
输出为:
i want to
i like china
怎么解决呢????
------解决方案--------------------------------------------------------
- Java code
for(int i=0;i<a.length;i++){ for(int j=0;j<b.length;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } //改为for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); }