I have to calculate the hits in a combination and I don't know how to do it. Some help?
The statement is:
This method will help us to calculate what prize the player user has obtained. To do this, we must pass the two arrays that we calculated in the two previous methods. Inside the method, we must compare the first 6 values of the array that the user bet with the first 6 values of the array values that came out in the random pot. The last value of each array is used for the refund that has a prize value different from the others and therefore must be treated separately. Every time a successful value is detected, it will be added to those that it might already have, the prize must be accumulated in a variable to later be returned at the end of the method.
And this is the code I have:
private void init(){
System.out.println("***** PRIMITIVA ******");
int[] apuesta = introducirApuesta();
int[] combinacionGanadora = calcularCombinacionGanadora();
if (combinacionGanadora!=null) {
System.out.println("La combinacion ganadora es: ");
for (int i = 0; i < combinacionGanadora.length - 1; i++) {
System.out.print(combinacionGanadora[i] + " ");
}
System.out.println("Reintegro: " + combinacionGanadora[combinacionGanadora.length - 1]);
}
int premio = comprobarAciertos(apuesta, combinacionGanadora);
System.out.println("Tu premio es: "+premio+" €");
}
public int[] introducirApuesta(){
Random rnd=new Random();
Scanner input=new Scanner(System.in);
int random = 0;
System.out.println("Introduce tu apuesta");
int [] apuesta=new int[6];
for (int i=0;i<apuesta.length;i++){
apuesta[i]=input.nextInt();
}
System.out.println("La Apuesta introducida es");
random=rnd.nextInt(9);
System.out.println(Arrays.toString(apuesta)+" "+random);
return apuesta;
}
public boolean disponible(int numero, int[] tomados){
for(int i = 0; i < tomados.length; i++){
if(numero == tomados[i]){
return false;
}
}
return true;
}
public int[] calcularCombinacionGanadora(){
int [] combinacion=new int[6];
int numero;
//Inicializando el array
for(int i=0; i < 6; i++){
combinacion[i] = -1;
}
//Rellenando el array
for(int i=0; i < 6; i++){
numero = (int)(Math.random() * 49 + 1);
while(!disponible(numero, combinacion)){
numero = (int)(Math.random() * 49 + 1);
}
combinacion[i] = numero;
}
combinacion[5] = (int)(Math.random() * 10);
return combinacion;
}
public int comprobarAciertos(int[] apuesta, int[] combinacionGanadora) {
int premio = 0;
int aciertos = 0;
//Comprobar aciertos en combinación
for (int i=0;i<apuesta.length -1;i++) {
if ( apuesta[0] == combinacionGanadora[0] ) {
premio++;
}
}
boolean reintegro = (apuesta[6] == combinacionGanadora[6]);
return premio;
}
}
Hello, good afternoon, I leave you my code where I validate the successes, I hope it will be useful to you.
when the refund is the same, it is assigned a value of 2 to differentiate it
screen output
***** PRIMITIVE ****** Enter your bet 2 22 32 25 24 29 The entered Bet is [2, 22, 32, 25, 24, 29] 2 The winning combination is: 24 48 29 6 31 Reimbursement : 1 Your prize is: 1 €
as you can see enter the 29 which appears in the winning combination.
If I did not misunderstand the question, each match with the array of the winning combination adds a prize to the array that has been bet.
then inside the
for