Aleatorios Nao Repetidos

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Aleatorios Nao Repetidos as PDF for free.

More details

  • Words: 276
  • Pages: 2
import import import import

java.util.ArrayList; java.util.Collections; java.util.LinkedList; java.util.Queue;

public class Tabuleiro { private int tabuleiro; private int largura; private void setLargura () { largura = largura(); } public int getLargura () { return largura; } public void setTabuleiro (int n) { tabuleiro = n; } public int getTabuleiro () { return tabuleiro; } // define a largura do tabuleiro int largura () { int n = 1; int total = 0 ; while (total != getTabuleiro()) { total = n*n; ++n; } return n-1; } // m�todo que retorna n�meros aleat�rios n�o repetidos public int [][]aleatorio () { setLargura(); int []tabUm = new int [getTabuleiro()]; Queue fila = preencheParaSorteio(getTabuleiro());// chama o m�todo aleat�rio for (int i = 0; i < tabUm.length; i++) { Integer n = fila.poll(); tabUm[i]= n; } int [][] tabDois = new int [getLargura()][getLargura()]; int cont = 0; for (int l = 0; l < tabDois.length; l++) { for (int c = 0; c < tabDois.length; c++) { tabDois[l][c] = tabUm[cont++]; } } imprima(tabUm);

}

return tabDois;

//m�todo aleat�rio onde os n�meros n�o se repetem public Queue preencheParaSorteio (int numeroDeElementos ) { ArrayList list = new ArrayList (); for (int i = 1; i <= numeroDeElementos ;i++ ) { // preenche a lista na ordem list.add(i); } // embaralha os elementos Collections.shuffle (list); // retorna a queue mais simples return new LinkedList (list); } // m�todo de teste void imprima (int t[]) { int cont = 0; for (int i = 0; i < t.length; i++) { if (cont < getLargura()-1) { System.out.print(" "+t[i]); cont++; } else { System.out.println(" " + t[i]); cont =0; } } } }

Related Documents

Enemigos Aleatorios
November 2019 12
Numeros Aleatorios
November 2019 21
Vectores Aleatorios
August 2019 14
Repetidos En Una Lista
November 2019 3