import java . util . Random ;
import java . util . Scanner ;
public class Loteria
{
private static String combinacionIntroducida ;
private static int combinacionIntro [] = new int [ 6 ] ;
private static int combinacionGanadora [] = new int [ 6 ] ;
private static Scanner entrada = new Scanner ( System . in ) ;
private static boolean incorrecto = true ;
private static boolean numCompletos = false ;
public static void obtenerCombinacion ()
{
System . out . println ( "\nIngrese 6 numeros separados por comas ( rango -> 1 - 46 ) : " ) ;
combinacionIntroducida = entrada . nextLine () ;
combinacionIntroducida = String . format ( "%s" , combinacionIntroducida + "," ) ;
}
public static void jugar ()
{
do
{
try
{
obtenerCombinacion () ;
convertir () ;
incorrecto = false ;
if ( ! numCompletos )
System . out . println ( "\nDebe ingresar 6 numeros!\n " ) ;
else if ( repetidoIntro () )
{
System . out . println ( "\nDebe ingresar 6 numeros distintos! \n" ) ;
incorrecto = true ;
}
}
catch ( NumberFormatException exepcion )
{
System . out . println ( "\nDebe ingresar datos correctos! \n" ) ;
incorrecto = true ;
}
catch ( ArrayIndexOutOfBoundsException excepcion )
{
System . out . println ( "\nDebe ingresar 6 numeros!\n " ) ;
incorrecto = true ;
}
}
while ( incorrecto || ! numCompletos ) ;
generarCombinacion () ;
imprimir () ;
comparar () ;
}
public static void convertir () throws NumberFormatException
{
String num = "" ;
int pos = 0 ;
for ( int i = 0 ; i < combinacionIntroducida . length () ; i ++ )
{
if ( combinacionIntroducida . charAt ( i ) != ',' )
{
num = String . format ( "%s" , num + combinacionIntroducida . charAt ( i ) ) ;
}
else if ( combinacionIntroducida . charAt ( i ) == ',' )
{
num = num . trim () ;
int numero = Integer . parseInt ( num ) ;
if ( numero > 41 || numero < 1 )
{
throw new NumberFormatException () ;
}
combinacionIntro [ pos ] = Integer . parseInt ( num ) ;
pos ++ ;
if ( pos == 6 )
numCompletos = true ;
num = "" ;
}
}
}
public static void generarCombinacion ()
{
Random random = new Random();
for ( int i = 0 ; i < 6 ; i ++ )
{
int num ;
do
{
num = ( random . nextInt ( 49 ) + 1 ) ;
}
while ( repetido ( num ) ) ;
combinacionGanadora [ i ] = num ;
}
}
public static boolean repetido ( int num )
{
for ( int ele : combinacionGanadora )
{
if ( ele == num )
{
return true ;
}
}
return false ;
}
private static boolean repetidoIntro ()
{
for ( int i = 0 ; i < 6 ; i ++ )
{
for ( int j = i + 1 ; j < 6 ; j ++ )
{
if ( combinacionIntro [ i ] == combinacionIntro [ j ] )
return true ;
}
}
return false ;
}
public static void comparar ()
{
int aciertos = 0 ;
for ( int combi : combinacionGanadora )
for ( int introdu : combinacionIntro )
if ( combi == introdu )
aciertos ++ ;
if( aciertos != 6 )
System . out . println ( "\n Numero de aciertos = " + aciertos ) ;
else if ( aciertos == 6 )
System . out . println ( "\n\n USTED GANO!\n\n " ) ;
}
public static void imprimir ()
{
System . out . print ( "\n Combinacion Introducida: " ) ;
for ( int el : combinacionIntro )
System . out. print ( el + " " ) ;
System . out . print ( "\n Combinacion Ganadora: " ) ;
for ( int ele : combinacionGanadora )
System . out. print ( ele + " " ) ;
}
public static void main ( String args [] )
{
jugar () ;
}
}