Last active
September 26, 2016 21:01
-
-
Save drmcarvalho/2db8f9408be455b5acb6a806575efa97 to your computer and use it in GitHub Desktop.
Classe que representa a estrutura do jogo MegaSena
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Random; | |
public class MegaSena | |
{ | |
private final int quantidadeNumeroApostado; | |
private final int[] numerosSorteados; | |
private int[] numerosApostados; | |
public MegaSena(int quantidadeNumeroApostado) | |
{ | |
this.quantidadeNumeroApostado = quantidadeNumeroApostado; | |
numerosApostados = new int[this.quantidadeNumeroApostado]; | |
numerosSorteados = new int[6]; | |
} | |
public int[] getNumerosApostados() | |
{ | |
Arrays.sort(numerosApostados); | |
return numerosApostados; | |
} | |
public void setNumerosApostados(int[] numerosApostados) | |
{ | |
this.numerosApostados = numerosApostados; | |
} | |
public int[] getNumerosSorteados() | |
{ | |
Arrays.sort(numerosSorteados); | |
return numerosSorteados; | |
} | |
public int apostaValida(int[] numeros) | |
{ | |
for (int i = 0; i < numeros.length; i++) | |
{ | |
if ((numeros[i] <= 0) || (numeros[i] > 60)) | |
{ | |
return numeros[i]; | |
} | |
} | |
return 0; | |
} | |
private boolean pesquisaLinear(int[] vetor, int numeroPesquisado) | |
{ | |
for (int i = 0; i < vetor.length; i++) | |
{ | |
if (vetor[i] == numeroPesquisado) | |
return true; | |
} | |
return false; | |
} | |
private int sortearNumero(int inicial, int nfinal) | |
{ | |
Random random = new Random(System.currentTimeMillis()); | |
int numeroSorteado = random.nextInt(nfinal - inicial) + inicial; | |
return numeroSorteado; | |
} | |
public void sorteia() | |
{ | |
int numeroSorteado; | |
int c = 0; | |
while (c < numerosSorteados.length) | |
{ | |
numeroSorteado = sortearNumero(1, 60); | |
if (!pesquisaLinear(numerosSorteados, numeroSorteado)) | |
{ | |
numerosSorteados[c] = numeroSorteado; | |
c++; | |
} | |
} | |
} | |
public int obtemAcertos() | |
{ | |
int acertos = 0; | |
int k = 0; | |
while (k < quantidadeNumeroApostado) | |
{ | |
if (pesquisaLinear(numerosSorteados, numerosApostados[k])) | |
{ | |
acertos++; | |
if (acertos == 6) | |
{ | |
return acertos; | |
} | |
} | |
k++; | |
} | |
return acertos; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Dener, Lucas | |
*/ | |
public class MegaSenaDesktop | |
{ | |
public static void main(String[] args) | |
{ | |
MegaSena ms = new MegaSena(7); | |
ms.setNumerosApostados(new int[] {11, 41, 52, 56, 57, 59, 60}); | |
ms.sorteia(); | |
int[] numerosSorteados = ms.getNumerosSorteados(); | |
int[] numerosApostados = ms.getNumerosApostados(); | |
int numeroApostadoValido = ms.apostaValida(numerosApostados); | |
if (numeroApostadoValido == 0) | |
{ | |
int acertos = ms.obtemAcertos(); | |
System.out.println("Numeros sorteados:"); | |
for (int i = 0; i < numerosSorteados.length; i++) | |
{ | |
System.out.print(numerosSorteados[i] + " "); | |
} | |
System.out.println("\n\nNumeros apostados:"); | |
for (int i = 0; i < numerosApostados.length; i++) | |
{ | |
System.out.print(numerosApostados[i] + " "); | |
} | |
System.out.println("\n\nAcertos: " + acertos); | |
} | |
else | |
{ | |
System.out.println("Aposta invalida devido ao numero apostado: " + numeroApostadoValido); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment