Java - ayuda con este ejercicio

 
Vista:

ayuda con este ejercicio

Publicado por Antoni (1 intervención) el 13/05/2019 12:32:20
quien puede ayudarme con este ejercicio que no consigo sacarlo

programa con diseño modular que lea una secuencia de 10 valores enteros
comprendidos entre el 1 y el 50 introducidos por teclado y los guarde en un array, la ordene de
manera ascendente y busque dentro del array si se encuentra un entero que generará el
programa de forma aleatoria entre el 1 y el 50.
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
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

[Solucion] ayuda con este ejercicio

Publicado por Billy Joel (875 intervenciones) el 23/05/2019 01:06:33
No estoy seguro si así de módulado tiene que estar la solución
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
 
public class Ejercicio{
 
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
    public static String leer(String message) {
        String s;
        try {
            System.out.print(message);
            s = br.readLine();
        } catch (IOException ex) {
            System.out.println("Hubo un error de lectura, vuelva a intentar");
            s = null;
        }
        if (s == null) {
            s = leer(message);
        }
        return s;
    }
 
    public static Integer leerInteger(String message) {
        Integer d;
        try {
            d = Integer.parseInt(leer(message));
 
        } catch (NumberFormatException ex) {
            System.out.println("Valor incorrecto vuelva a intentar");
            d = null;
        }
        if (d == null) {
            d = leerInteger(message);
        }
        return d;
    }
 
    public static Integer leerInteger_1_50(String message) {
        Integer d = leerInteger(message);
        if (d < 1 || d > 50) {
            System.out.println("Numero fuera de rango.\nIntroduzca solo números entre 1 y 50");
            d = leerInteger_1_50(message);
        }
        return d;
    }
 
    public static int[] leerArreglo() {
        int[] valores = new int[10];
        for (int i = 0; i < valores.length; i++) {
            valores[i] = leerInteger_1_50("Introduzca el valor " + (i + 1) + ": ");
        }
        return valores;
    }
 
    public static void ordenamientoBurbuja(int[] valores) {
        for (int i = valores.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (j + 1 <= i && valores[j] > (valores[j + 1])) {
                    int aux = valores[j];
                    valores[j] = valores[j + 1];
                    valores[j + 1] = aux;
                }
            }
        }
    }
 
    public static Integer getRandomNumber(int min, int max) {
        return new Random().nextInt(max - min + 1) + min;
    }
 
    public static void main(String[] args) {
        int[] valores = leerArreglo();
        System.out.println("Valores antes de ordenar: ");
        for (int i = 0; i < valores.length; i++) {
            System.out.print(valores[i] + (i + 1 < valores.length ? ", " : "\n"));
        }
        ordenamientoBurbuja(valores);
        System.out.println("Valores ordenados: ");
        for (int i = 0; i < valores.length; i++) {
            System.out.print(valores[i] + (i + 1 < valores.length ? ", " : "\n"));
        }
        int numeroAleatorio = getRandomNumber(1, 50);
        boolean encontrado = false;
        for (int valor : valores) {
            if (valor == numeroAleatorio) {
                encontrado = true;
                break;
            }
        }
        System.out.println("El numero aleatorio " + numeroAleatorio + (encontrado ? " " : " no ") + "fue encontrado");
    }
}

Saludos!
Billy Joel
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