Then I have the following problem, I created a 30x60 matrix in which I will generate a number in a random position, so far so good. But when I get the function that generates the number and put it in the class, I'm having trouble calling it.
My code looks like this: I've omitted the matrix padding code to get smaller
int [][] matrix = new int[30][60];
Caminhao caminhoes = new Caminhao();
Carro carros = new Carro();
Moto motos = new Moto();
//Utilizando as funções com objetos
carros.gerarCarro();
motos.gerarMoto();
caminhoes.gerarCaminhao();
//Código para printar a matriz após cada veiculo adicionado
for(int x = 0; x < matrix.length; x++)
{
for (int i = 0; i < matrix[x].length; i++)
{
System.out.print(matrix[x][i]);
}
System.out.println();
}
And the code in the class that is in the function looks like this:
/Método para gerar um Carro em um lugar aleatório da matriz
public void gerarCarro(){
Random r= new Random();
int [][] matrix = new int[30][60];
int a =r.nextInt(29);
int b =r.nextInt(59);
if(matrix[a][b] == 0 && matrix[a][b] != 2 && matrix[a][b]!=1 && matrix[a][b]!=4 && matrix[a][b]!=5){
matrix[a][b]=3;
}
}
}
What's wrong?