Advertisement
Guest User

Ejercicio2

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package ejercicio2;
  2. import java.util.Scanner;
  3. public class Ejercicio2 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner lector = new Scanner(System.in);
  8.        
  9.         int[] numeros = new int[6];
  10.         int[] aleatorios = new int[6];
  11.         int comprobaciones;
  12.         boolean duplicado;
  13.        
  14.         introducir(lector, numeros);
  15.         rellenoAleatorios(aleatorios);
  16.         ordenar(numeros);
  17.         ordenar(aleatorios);
  18.        
  19.         duplicado = repetido(numeros);
  20.        
  21.         while(duplicado){
  22.             System.out.println("Ha introducido numeros repetidos, introduzca los numeros nuevos.");
  23.        
  24.         introducir(lector, numeros);
  25.         ordenar(numeros);
  26.         duplicado = repetido(numeros);
  27.         }
  28.         System.out.println("Numeros elegidos: ");
  29.        
  30.         listarVector(numeros);
  31.         comprobaciones = compararVector(numeros, aleatorios);
  32.         System.out.println();
  33.         System.out.println("Se han realizado " + comprobaciones + " intentos ");
  34.        
  35.     }
  36.     static void introducir (Scanner lector, int[] num){
  37.         for(int i=0;i<num.length;i++){
  38.            
  39.             System.out.println("Introduce un numero entre 1 y 49 "+ (i + 1) + ": ");
  40.             num[i] = lector.nextInt();
  41.            
  42.             if(num[i] > 49 || num[1] < 1){
  43.                 System.out.println("Introduzca un numero correcto");
  44.                 i--;
  45.             }
  46.         }
  47.     }
  48.     static boolean repetido (int[] numeros){
  49.        
  50.         for(int i=0;i<numeros.length;i++){
  51.             for(int j=i+1; j<numeros.length;j++){
  52.                 if(j != i && numeros[j] == numeros[i]){
  53.                     return true;
  54.                 }
  55.             }
  56.         }
  57.         return false;
  58.     }
  59.      static void rellenoAleatorios(int[] aleatorios){
  60.          for(int i=0;i<aleatorios.length;i++){
  61.              aleatorios[i] = (int)(Math.random()*49 + 1);
  62.          }
  63.     }  
  64.      static void ordenar(int[] v){
  65.          int aux;
  66.          
  67.          for(int i=0;i<v.length - 1;i++){
  68.              for(int j=0;j<v.length - 1; j++){
  69.                  if(v[j + 1] <= v[j]){
  70.                      aux = v[j + 1];
  71.                      v[j + 1] = v[j];
  72.                      v[j] = aux;
  73.                  }
  74.              }
  75.          }    
  76.     }
  77.      static void listarVector(int[] v){
  78.              for (int i=0;i<v.length;i++){
  79.                  System.out.println(v[i] + "");
  80.          }
  81.          
  82.     }
  83.      static int compararVector(int[] v1, int v2[]){
  84.              int j;
  85.              
  86.              int comparaciones = 0;
  87.              
  88.              do{
  89.                  j=0;
  90.                  
  91.                  rellenoAleatorios(v2);
  92.                  ordenar(v2);
  93.                  
  94.                  for(int i=0;i<v1.length;i++){
  95.                      if(v1[i] == v2[i]){
  96.                          j++;
  97.                  }
  98.                  
  99.                  
  100.              }
  101.                  comparaciones++;
  102.                  
  103.              }while(j != 6);
  104.              
  105.              return comparaciones;
  106.              }
  107.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement