Lottery, lottery exercise

0

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);        
}

}
    
asked by scorpions 07.06.2017 в 15:43
source

2 answers

0

I've been reviewing your code and had some errors for what you wanted to do: I think this would be the result of what you expect:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import java.util.Random;

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=0;i<numeros.length;i++){
           indice = (int)(Math.random()*(bombo.size()-i));
           numeros[i] = bombo.remove(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;
                }else{
                    ordenados[j] = j;
                }
            }
        }
        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); 
    }
}

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";
    }

}

class Loteria {

    public static void main(String[] args) {
        Apuestas apuestas = new Apuestas();

        Apuesta apuesta = null;

        Apuesta sorteo = new Apuesta();

        System.out.println("Apuestas realizadas para el sorteo: "+sorteo);
        for(int i=0;i<=10;i++){
           apuesta = new Apuesta();
           apuesta.generaApuesta();
           apuestas.insertarApuesta(apuesta);
           apuesta.ordenar();

            int acertados = apuesta.acertados(sorteo.getNumeros());

            System.out.println("\t"+apuesta+" Acertó " + acertados+" números");        

        }
    }
}

Produce this example output

Apuestas realizadas para el sorteo: [44, 34, 18, 15, 25, 39]
    [3, 9, 10, 19, 39, 39] Acertó 2 números
    [20, 27, 34, 43, 45, 47] Acertó 1 números
    [0, 13, 34, 36, 44, 46] Acertó 2 números
    [1, 16, 16, 17, 30, 39] Acertó 1 números
    [11, 13, 15, 21, 23, 37] Acertó 1 números
    [3, 16, 20, 23, 26, 35] Acertó 0 números
    [4, 4, 6, 8, 34, 49] Acertó 1 números
    [0, 19, 24, 27, 35, 38] Acertó 0 números
    [3, 24, 27, 30, 42, 49] Acertó 0 números
    [13, 19, 21, 38, 43, 46] Acertó 0 números
    [9, 14, 32, 34, 34, 47] Acertó 2 números
    
answered by 07.06.2017 / 16:43
source
0

I do not know exactly what part of the code wants to do. I indicate by means of comments the changes that would introduce and the doubts that arise to me:

BET CLASS:

import java.util.ArrayList;
import java.util.Arrays;


public class Apuesta {

    int[] numeros = new int[6];
    int nAciertos;

/* Creo que sobraría, simplemente tienes una lista que sabes que
es de 49 números (esto podrías definirlo como una constante)
en la que la posición i tiene valor i */
    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++){

/*No entiendo el 48-i. No te asegura que no haya repetidos si es eso lo que
quieres, lo único que consigue es que el segundo número sea menor que 48,
el tercero menor que 47...*/
           indice = (int)(Math.random()*(48-i));

   // ¿En vez de esto, donde igualas el indice arriba, igualar numeros [i]?
           numeros[i] = bombo.get(indice);
       }
    }

    public int[] getNumeros(){
        return numeros;
    }

    public int[] ordenar(){

/* No entiendo nada del método. Con el .sort, suficiente.
¿El resto para qué es? ¿Y lo de incrementar el número de aciertos?
¿No hay un método que se encarga de eso ya? */

  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++;
// Se va a ganar muy poca eficiencia, pero un break aquí sería lo suyo
                }
            }
        }
    return nAciertos;
    }

    @Override
    public String toString(){
/* acertados(getNumeros) va a comparar cuantos hay iguales
entre numeros y numeros, ¿no?*/
        return Arrays.toString(numeros) + "\n"+ acertados(getNumeros()); 
    }
}

CLASS BETS:

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();

/* ¿Sería sorteo.acertados(apuesta.getNumeros())?
Estas dos líneas no deberían ser dentro de un for
que recorriese cada apuesta dentro de apuestas?
int acertados = apuesta.acertados(sorteo.getNumeros()); */

    System.out.println("Los numeros acertados son: " + acertados);        
}

}

If something is not clear to you, or you want to clarify something, go ahead.

    
answered by 07.06.2017 в 16:41