SHOW:
|
|
- or go back to the newest paste.
1 | import java.util.Scanner; | |
2 | ||
3 | public class Punto2 { | |
4 | ||
5 | public static void main(String[] args) { | |
6 | Scanner consola = new Scanner(System.in); | |
7 | int dimension = Modulo.validarNumero(consola, "Ingrese la dimension del vector."); | |
8 | int[] arreglo = new int[dimension]; | |
9 | int opc = -1; | |
10 | while (opc != 3) { | |
11 | menu(); | |
12 | opc=Modulo.validarNumero(consola, "Ingrese una opcion:"); | |
13 | switch(opc) { | |
14 | case 1: | |
15 | arreglo=cargaVector(arreglo,dimension,consola); | |
16 | mostrar(arreglo, dimension); | |
17 | arreglo=revertir(arreglo); | |
18 | System.out.println("---El arreglo revertido---"); | |
19 | mostrar(arreglo, dimension); | |
20 | contarPar(arreglo, dimension); | |
21 | break; | |
22 | case 2: | |
23 | arreglo=cargaRandom(arreglo,dimension); | |
24 | mostrar(arreglo, dimension); | |
25 | arreglo=revertir(arreglo); | |
26 | System.out.println("---El arreglo revertido---"); | |
27 | mostrar(arreglo, dimension); | |
28 | contarPar(arreglo, dimension); | |
29 | break; | |
30 | case 3: | |
31 | System.out.println("Cerrando programa..."); | |
32 | break; | |
33 | } | |
34 | ||
35 | ||
36 | } | |
37 | ||
38 | } | |
39 | ||
40 | public static int [] cargaVector(int [] vector, int dimension,Scanner ingreso) { | |
41 | int carga; | |
42 | boolean verificar = true; | |
43 | for (int i = 0; i<dimension; i++) { | |
44 | carga= Modulo.validarNumero(ingreso, "ingrese un numero multiplo de 5 y no de 3"); | |
45 | verificar = primos(carga); | |
46 | if (verificar == true) { | |
47 | vector[i]=carga; | |
48 | } | |
49 | else { | |
50 | System.out.println("El numero ingresado es multiplo de 3."); | |
51 | i=i-1; | |
52 | } | |
53 | } | |
54 | return vector; | |
55 | } | |
56 | ||
57 | public static int [] cargaRandom(int [] vector, int dimension) { | |
58 | boolean verificar = true; | |
59 | for (int i = 0; i<dimension; i++) { | |
60 | int carga=(int) (Math.random()*100); | |
61 | verificar = primos(carga); | |
62 | if (verificar == true) { | |
63 | System.out.println("Valor generado por random: "+carga); | |
64 | vector[i]=carga; | |
65 | } | |
66 | else { | |
67 | i=i-1; | |
68 | } | |
69 | } | |
70 | return vector; | |
71 | } | |
72 | ||
73 | ||
74 | ||
75 | public static boolean primos(int x) { | |
76 | if (x%5==0 & x%3!=0) { | |
77 | return true; | |
78 | } | |
79 | return false; | |
80 | ||
81 | } | |
82 | ||
83 | public static int [] mostrar(int [] vector, int dimension) { | |
84 | for (int i=0; i<dimension;i++) { | |
85 | System.out.println("El valor del vector es: "+ vector[i]); | |
86 | } | |
87 | return vector; | |
88 | } | |
89 | ||
90 | public static void contarPar(int []vector, int dimension) { | |
91 | int contPar=0,contImp=0; | |
92 | for (int i=0; i<dimension; i++) { | |
93 | if (vector[i]%2==0) { | |
94 | contPar=contPar + 1 ; | |
95 | } | |
96 | else { | |
97 | contImp=contImp + 1; | |
98 | } | |
99 | } | |
100 | System.out.println("Cantidad de pares: "+contPar); | |
101 | System.out.println("Cantidad de impares: "+contImp); | |
102 | ||
103 | ||
104 | } | |
105 | ||
106 | public static int[] revertir(int[] vector) { | |
107 | int aux; | |
108 | for (int i = 0; i < vector.length / 2; i++) { | |
109 | aux = vector[i]; | |
110 | vector[i] = vector[vector.length - 1 - i]; | |
111 | vector[vector.length - 1 - i] = aux; | |
112 | } | |
113 | ||
114 | return vector; | |
115 | } | |
116 | ||
117 | ||
118 | public static void menu() { | |
119 | System.out.println("***Menu Principal***"); | |
120 | System.out.println("1 - Ingreso de valores manualmente."); | |
121 | System.out.println("2 - Generar valores random."); | |
122 | System.out.println("3 - Salir."); | |
123 | } | |
124 | ||
125 | ||
126 | } |