Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main2 {
- public static void main(String[] args) {
- Arreglo arregloDatos = new Arreglo();
- String[] options = {
- "Ingresar datos por consola",
- "Generar datos aleatorios"
- };
- Menu menu = new Menu(options);
- boolean repeat = true;
- while(repeat) {
- int option = menu.getOption();
- switch (option) {
- case 1:
- arregloDatos.setDimension(Helper.getPositiveInt("Ingrese el tamaño del arreglo: "));
- ingresoDatosConsola(arregloDatos);
- mostrarResultados(arregloDatos);
- break;
- case 2:
- arregloDatos.setDimension(Helper.getPositiveInt("Ingrese el tamaño del arreglo: "));
- generarDatosAleatorio(arregloDatos);
- mostrarResultados(arregloDatos);
- break;
- case 0:
- repeat = false;
- System.out.println("Fin de programa");
- continue;
- }
- Helper.pressEnterKeyToContinue();
- }
- }
- // En este procedimiento se ingresan valores por consola y se guardan solo los
- // que cumplen con la condicion
- public static void ingresoDatosConsola(Arreglo arregloDatos) {
- Integer[] arreglo = new Integer[arregloDatos.getDimension()];
- Integer num, i = 0;
- while (i < arregloDatos.getDimension()) {
- num = Helper.getInt("Ingrese dato " + (i + 1) + " :");
- if (validarCondicionMultiplos(num)) {
- arreglo[i] = num;// Se guarda solo los numeros que han sido verificados que cumplen con la
- // condicion de multiplos
- i++;
- } else {
- System.err.println(
- "El numero no cumple con la condicion de que sea multiplo de 5 y a la vez no sea multiplo de 3.\nIntente de nuevo.");
- }
- }
- arregloDatos.setArreglo(arreglo);
- }
- // Valida que el numero ingresado sea multiplo de 5 pero no de 3
- public static boolean validarCondicionMultiplos(int num) {
- return num % 5 == 0 && num % 3 != 0;
- }
- // En este procedimiento se generan valores aleatorios y se guardan solo los que
- // cumplen con la condicion
- public static void generarDatosAleatorio(Arreglo arregloDatos) {
- int n = arregloDatos.getDimension();
- Integer intArray[] = new Integer[n];
- for (int i = 0; i < n; i++) {
- int number = Helper.randomInt(1, 100) * 5;
- if (MathUtils.isMultipleOf(number, 3)) number += 5;
- intArray[i] = number;
- }
- arregloDatos.setArreglo(intArray);
- }
- public static void mostrarResultados(Arreglo arregloDatos) {
- System.out.println("Datos ingresados: ");
- ArrayUtils.print(arregloDatos.getArreglo());
- System.out.println("Datos ingresados invertidos: ");
- Object [] reverseArray = ArrayUtils.reverse(arregloDatos.getArreglo());
- ArrayUtils.print(reverseArray);
- arregloDatos.mostrarParImpar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement