[1] [2] [3]线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:3在Matrices.main(Matrices.java:19)[重复
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[1] [2] [3]线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:3在Matrices.main(Matrices.java:19)[重复相关的知识,希望对你有一定的参考价值。
我正在做一个java课程,我试图在没有帮助的情况下进行这项练习,但我的cdm对我说:
[1] [2] [3]线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:3在Matrices.main(Matrices.java:19)即便如此,我复制了我的老师的练习,他没有任何错误。请帮帮我。谢谢。
我的运动:
import java.util.Scanner;
public class Matrices{
public static void main(String args[]){
int contador = 1, filas = 0, columnas = 0;
Scanner entrada = new Scanner(System.in);
System.out.println("¿Cuantas filas quiere?");
filas = entrada.nextInt();
System.out.println("¿Cuantas columnas quiere?");
columnas = entrada.nextInt();
int matriz1 [][] = new int [filas][columnas];
for(int j = 0; j < filas; j++){
for(int i = 0; i < columnas; j++){
matriz1[j][i] = contador;
contador++;
System.out.print("[" + matriz1[j][i] + "]");
}
System.out.println("");
}
}
}
我的老师练习:
import java.util.Scanner;
public class MatricesDinamicas{
public static void main(String args[]){
int filas = 0, columnas = 0, contador = 1;
Scanner entrada = new Scanner(System.in);
System.out.println("¿Cuantas filas deseas?");
filas = entrada.nextInt();
System.out.println("¿Cuantas columnas deseas?");
columnas = entrada.nextInt();
int numeros [][] = new int [filas][columnas];
for(int j = 0; j < filas; j++){
for(int i = 0; i < columnas; i++){
numeros[j][i] = contador;
contador++;
System.out.print("[" + numeros[j][i] + "]");
}
System.out.println("");
}
}
}
答案
你在j
的循环中递增i
:
for(int j = 0; j < filas; j++) {
for(int i = 0; i < columnas; i++) {
// Was j in the OP ------^
以上是关于[1] [2] [3]线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:3在Matrices.main(Matrices.java:19)[重复的主要内容,如果未能解决你的问题,请参考以下文章