1
So 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 took the function that generates the number and put it in the class, I’m having trouble calling it.
My code is like this: I omitted the matrix fill code to make it 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 the function is 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 is wrong ?
But what is the purpose of what you are trying to do ? that method
gerarCarro
fill in the matrix you have in main ? Is that you are creating a new one within the methodgerarCarro
.– Isac
The generated method has to generate the number 3 at a random position of the matrix that has the value of 0.
– Caio Sousa