Hola a todos,
elementos usados:
Escáner – Entrada de usuario
Generar Random – Números aleatorios
Exception try-catch
Label-continue
Bucle foreach
BigInteger para números muy grandes
Metodos
public class loteria {
public static void main(String[] args){
int givenValue[] = new int[6];
int randomValue[] = new int[6];
int hits = 0; //para números encontrados
givenValue = determineGivenNumbers(); //Metodo para UserInput
randomValue = determineRandomNumbers(); //Metodo para generar números aleatorios
hits = checkForEquality(givenValue, randomValue); //Comprueba los Arrays en igualdad
printArrays(givenValue, randomValue, hits); //Da los Arrays y los blancos hallados
binomialcoeficiente(); //Calcular la probabilidad de 6 blancos de 49
}
/*----------------------------UserInput---------------------------------------*/
public static int[] determineGivenNumbers(){
int givenValue[] = new int[6];
Scanner scan = new Scanner(System.in);
int checkGivNumber = 0; //Para que no se pueda ingresar números iguales
boolean numOrLetter = false;//Para que no se pueda ingresar letras
System.out.println("Please enter six numbers 1-49!");
Böö: for(int i=0; i<givenValue.length; i++){
do{
try{
numOrLetter = false;
checkGivNumber = scan.nextInt();
}catch(Exception e){
System.err.println("Please enter a valid number! " + e.toString());
scan.nextLine();
numOrLetter = true; //Error si se ingresa una letra; preguntar de nuevo
}
if(checkGivNumber <= 0 || checkGivNumber > 49) System.err.println("Please give a number in the specifield range!");
}while(checkGivNumber <= 0 || checkGivNumber > 49 || numOrLetter == true);
for(int j=0; j<i; j++){
if(givenValue[j] == checkGivNumber){ //Por si el número se ingresó dos veces
System.err.println("The entered number is already exists!");
System.out.println("Please enter a new number!");
i--; //Entonces cuando el mismo número no sigue iterando el bucle for
continue Böö; //Entonces ve hacia Böö, entonces despues de continue no se ejecuta mas nada
}
}
givenValue[i] = checkGivNumber;
}
Arrays.sort(givenValue);
return givenValue;
}
/*----------------------------------------------------------------------------*/
/*--------------------------Random Números aleatorios----------------------------*/
public static int[] determineRandomNumbers(){
int randomValue[] = new int[6];
Random rNumber = new Random();
int checkRanNumber = 0; //Para que no se generen números random iguales
Böö: for(int i=0; i<randomValue.length; i++){
checkRanNumber = rNumber.nextInt(49)+1;
for(int j=0; j<i; j++){
if(randomValue[j] == checkRanNumber){
i--;
continue Böö;
}
}
randomValue[i] = checkRanNumber;
}
Arrays.sort(randomValue);
return randomValue;
}
/*----------------------------------------------------------------------------*/
/*------------------------Comprobar Arrays en igualdad------------------------*/
public static int checkForEquality(int givenValue[], int randomValue[]){
int hits = 0;
//Comprobar en Array cuantos números son iguales
for(int j=0; j<randomValue.length; j++){
for(int i=0; i<givenValue.length; i++){
if(givenValue[i] == randomValue[j]){
hits++;
}
}
}
return hits;
}
/*----------------------------------------------------------------------------*/
/*-----------------Dar los Arrays y los blancos hallados -------------------*/
public static void printArrays(int givenValue[], int randomValue[], int hits){
for(int i : givenValue){
System.out.print(i + "\t");
}
System.out.println();
for(int i : randomValue){
System.out.print(i + "\t");
}
System.out.println("You have " + hits + " hits!");
}
/*----------------------------------------------------------------------------*/
/*-----------Calcular la probabilidad de 6 de 49 blancos ------------*/
public static void binomialcoeficiente(){
BigInteger result = BigInteger.ONE;
int n = 49;
int k = 6;
result = faculty(n).divide(faculty(k).multiply((faculty(n-k))));
System.out.println("The propability for six correct hits are 1 : " + result);
double d1 = result.doubleValue(); //Convertir el valor BigIntegerden en double
System.out.println(1/d1 + " %"); //Para que pueda ser especificado en porción %
}
//Al mismo tiempo los objetos BigInteger se harán tan grandes,
//como los respectivos resultados necesitan espacio
public static BigInteger faculty(int number){
if(number <= 1) return BigInteger.ONE;
return BigInteger.valueOf(number).multiply(faculty(number-1));
//BigInteger.valueOf(long val) produce un BigInteger que acepta el valor val
//Con .multiply se hace this * multiply(número)
}
}
Saludos