Java - PROBLEMA JAVA URGENTE VIDA O MUERTE!

 
Vista:

PROBLEMA JAVA URGENTE VIDA O MUERTE!

Publicado por mac (1 intervención) el 06/03/2013 16:57:00
Escribir un programa que simule el juego de la lotería. Un módulo (método jugar) se encargará de pedir al usuario seis enteros entre el 1 y el 49 sin repetición,en forma de cadena de caracteres separados por comas, y los devolverá en un array, otro módulo (método sortear) generará un array de seis enteros al azar entre el 1 y el 49 sin repetición y por último el método comprobar dirá el número de aciertos.

Además hay que escribir un método auxiliar imprimir que permita imprimir los arrays de las combinaciones. El resultado del programa deberá verse como:

Introduzca su combinación:
22, 25, 31, 38, 41, 49
...
Combinación jugada: 22, 25, 31, 38, 41, 49
Combinaciónganadora: 8, 14, 22, 23, 30, 41
Número de aciertos: 2
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

PROBLEMA JAVA URGENTE VIDA O MUERTE!

Publicado por M2 (5 intervenciones) el 06/03/2013 18:46:24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
 
public class Primitiva {
    public static void main(String[] args) throws NumeroMal {
        int[] jugada = jugar();
        System.out.print("Tu jugada: ");
        imprimir(jugada);
 
        int[] sorteo = sortear();
        System.out.print("\nEl sorteo: ");
        imprimir(sorteo);
 
        System.out.println("\nHas acertado " + comprobar(jugada, sorteo));
    }
    public static final int NUMEROS = 6;
    public static Scanner entrada= new Scanner(System.in);
 
 
    public static int[] jugar() throws NumeroMal {
 
        int[] combinacion = new int[NUMEROS];
        try{
 
             for (int i=0; i<NUMEROS; i++) {
            do {
 
                System.out.print("introduzca un numero(" + i + ") entre el 1 y el 49: ");
 
                combinacion[i] = entrada.nextInt();
            } while ((combinacion[i] < 1) || (combinacion[i] > 49) || contiene(combinacion, combinacion[i], i));
 
            }throw new NumeroMal("no admite otros numeros.");
 
    }catch(NumeroMal nm){
 System.out.println("\n"+nm.getMessage());
}
 
//catch () {
 
   //             System.out.println("\nError de entrada/salida: " + ioe.toString());
 
//            } 
catch (Exception e) {
 
                // Captura cualquier otra excepción
 
                System.out.println("Excepción: " + e.toString());
 
            }
        return combinacion;
 
 
    }
 
 
 
    public static boolean contiene(int[] array, int numero, int rellenos) {
        boolean resultado = false;
 
        for (int i=0; i<rellenos;i++) {
            if (numero == array[i]) {
                resultado = true;
            }
        }
 
        return resultado;
    }
 
    public static int[] sortear() {
        int combinacion[] = new int[NUMEROS];
        Random random = new Random();
 
        for (int i=0; i<NUMEROS; i++) {
            do {
                combinacion[i] = random.nextInt(49)+1;
            } while (contiene(combinacion, combinacion[i], i));
        }
 
        return combinacion;
    }
 
    public static int comprobar(int[] combinacion1, int[] combinacion2) {
        int aciertos = 0;
 
        for (int i=0; i<NUMEROS; i++){
            if (contiene(combinacion2, combinacion1[i], NUMEROS)) {
                aciertos++;
            }
        }
 
        return aciertos;
    }
 
    public static void imprimir(int[] combinacion) {
        for (int i=0; i<combinacion.length; i++) {
            System.out.print(combinacion[i] + " ");
        }
    }
 
 
 
}
class NumeroMal extends Exception{
    public NumeroMal(){
        super("Excepcion definida por el usuario:NUMERO INCORRECTO");
 
    }
        public NumeroMal(String msg){
            super("Excepcion definida por el usuario:"+msg);
        }
 
 
   }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

PROBLEMA JAVA URGENTE VIDA O MUERTE!

Publicado por bryanne (26 intervenciones) el 06/03/2013 21:23:45
hola!

bueno ahi va el codigo !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 () ;
	}
 
 
}


Si tienes dudas, házmelo saber

Saludos !
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar