|
| 1 | +package unit05.Actividad09; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +/* |
| 7 | + Diseñar una aplicación para un campeonato de programación, donde se introducirá la puntuación de 5 |
| 8 | + programadores. La aplicación debe de mostrar la puntuación ordenada de los 5 programadores. En ocasiones |
| 9 | + cuando finalizan los 5 participantes anteriores se suman al campeonato programador de exhibición, cuyos |
| 10 | + puntos se incluyen con el resto. La forma de saber que no intervienen más programadores es insertando la |
| 11 | + puntuación de -1. La aplicación debe de mostrar los puntos ordenados de todos los participantes. |
| 12 | + */ |
| 13 | +public class Actividad9 { |
| 14 | + public static void main(String[] args) { |
| 15 | + int[] notasProgramadores = new int[5]; |
| 16 | + for (int i = 0; i < 5; i++) { |
| 17 | + int nota = getInt("Introduce la nota: "); |
| 18 | + notasProgramadores[i] = nota; |
| 19 | + } |
| 20 | + Arrays.sort(notasProgramadores); |
| 21 | + System.out.println("La puntuación de los 5 programadores es: "+Arrays.toString(notasProgramadores)); |
| 22 | + int notaExhibicion = getInt("Ingrese la nota de exhibición: "); |
| 23 | + while (notaExhibicion != -1){ |
| 24 | + notasProgramadores = insercionOrdenada(notasProgramadores,notaExhibicion); |
| 25 | + notaExhibicion = getInt("Ingrese la nota de exhibición: "); |
| 26 | + } |
| 27 | + System.out.println("Las notas totales son: "+Arrays.toString(notasProgramadores)); |
| 28 | + } |
| 29 | + |
| 30 | + public static int getInt(String mensaje) { |
| 31 | + Scanner sc = new Scanner(System.in); |
| 32 | + int numero; |
| 33 | + System.out.print(mensaje); |
| 34 | + while (!sc.hasNextInt()){ |
| 35 | + System.out.println("Ingrese un valor numérico."); |
| 36 | + System.out.print(mensaje); |
| 37 | + sc.nextLine(); |
| 38 | + } |
| 39 | + numero = sc.nextInt(); |
| 40 | + return numero; |
| 41 | + } |
| 42 | + |
| 43 | + public static int[] insercionOrdenada (int[] t, int nuevoValor){ |
| 44 | + int[] nuevoArray = new int[t.length+1]; |
| 45 | + int indiceInsercion = Arrays.binarySearch(t,nuevoValor); |
| 46 | + if (indiceInsercion < 0) { |
| 47 | + indiceInsercion = -indiceInsercion -1; |
| 48 | + } |
| 49 | + System.arraycopy(t,0,nuevoArray,0,indiceInsercion); |
| 50 | + System.arraycopy(t,indiceInsercion,nuevoArray,indiceInsercion+1,t.length-indiceInsercion); |
| 51 | + nuevoArray[indiceInsercion] = nuevoValor; |
| 52 | + return nuevoArray; |
| 53 | + } |
| 54 | +} |
0 commit comments