I am involved in a small program that calculates a Lottery.
Let me explain, this I am developing in java.
We have a betting class with a% co_of% of bets. We have a bet class, where we have a hype of random numbers ranging from 1 to 49, and where I have different methods. Among them there is one where I generate random bets and where I verify that they have been successful. Then there is a Main method where I make the calls and make the appropriate exits. My doubt is that I do not know how to compare the content of the arrays, and even if my methods are correct, because I get them to show me the bets but not the right ones. These are an array of 6 elements or I had thought so, although I am content to return if an element of the raffle array is in my array of the bet. I leave my classes and all the code to see if you lucubrarais a little.
PS: I know it's a simple exercise, but I'm starting with programming, and simple things like this cost me much more than generating an application with GUI that manages a database for example ...
Thanks in advance.
CODE:
BET CLASS:
import java.util.ArrayList;
import java.util.Arrays;
public class Apuesta {
int[] numeros = new int[6];
int nAciertos;
ArrayList<Integer> bombo = new ArrayList<Integer>();
public Apuesta(){
generaApuesta();
}
public void generaApuesta(){
for(int i=0; i<=49;i++){
bombo.add(i);
}
/*Llenamos el array de apuestas*/
int indice;
for(int i=1;i<numeros.length;i++){
indice = (int)(Math.random()*(48-i));
numeros[i] = bombo.get(indice);
}
}
public int[] getNumeros(){
return numeros;
}
public int[] ordenar(){
int[] ordenados = new int[6];
Arrays.sort(numeros);
for(int i=0; i<numeros.length;i++){
for(int j=0; j<bombo.size();j++){
if(i<j){
ordenados[i] = i;
nAciertos++;
}else{
ordenados[j] = j;
nAciertos++;
}
}
}
return ordenados;
}
public int acertados(int[] sorteo){
for(int i=0; i<sorteo.length;i++){
for(int j=0;j<numeros.length;j++){
if(sorteo[i] == numeros[j]){
nAciertos++;
}
}
}
return nAciertos;
}
@Override
public String toString(){
return Arrays.toString(numeros) + "\n"+ acertados(getNumeros());
}
}
BETS CLASS:
import java.util.ArrayList;
public class Apuestas {
ArrayList<Apuesta> apuestas = new ArrayList<Apuesta>();
Apuesta apuesta;
public void insertarApuesta(Apuesta a){
apuestas.add(a);
}
@Override
public String toString(){
String salida = "";
for(int i=0; i<apuestas.size();i++){
salida += i;
}
return apuestas.toString() + "\n";
}
}
MAIN
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class Loteria {
public static void main(String[] args) {
Apuestas apuestas = new Apuestas();
Apuesta apuesta = null;
for(int i=0;i<=10;i++){
apuesta = new Apuesta();
apuesta.generaApuesta();
apuestas.insertarApuesta(apuesta);
apuesta.ordenar();
}
Apuesta sorteo = new Apuesta();
System.out.println("Las apuestas son: " + apuestas.toString());
sorteo.getNumeros();
int acertados = apuesta.acertados(sorteo.getNumeros());
System.out.println("Los numeros acertados son: " + acertados);
}
}