Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-UNIVERSIDAD NACIONAL DE JUJUY- FACULTAD DE INGENIERÍA -ESTRUCTURAS DE DATOS
- //-TRABAJO PRACTICO N°1: ARREGLOS, ARREGLOS DINÁMICOS, OBJETOS BÁSICOS -GRUPO 3.2
- //-------------------------------------------------------------------------------------------------------
- import java.util.Random;
- import java.util.Scanner;
- public class EJERC2 {
- public static void main(String[] args) {
- Scanner sc =new Scanner(System.in);
- int OP;
- int dim=PositivoInt(sc,"Ingrese la dimensión del arreglo: ");
- int V[]=new int [dim];
- do {
- OP=Menu(sc,"Ingrese una opción del menú: ");
- System.out.println("");
- switch (OP){
- case 1:
- cargarVector(sc,V);
- Mostrar(V);
- MostrarInvertido(V);
- Pares(V);
- break;
- case 2:
- VectorAleatorio(V);
- Mostrar(V);
- MostrarInvertido(V);
- Pares(V);
- break;
- case 3:
- System.out.println("MUCHAS GRACIAS!!, HASTA LUEGO.");
- break;
- default:
- System.out.println("Opción no valida");
- }
- } while (OP!=3);
- sc.close();
- }
- public static int Menu(Scanner sc,String message ){
- int opcion;
- do {
- System.out.println("-----------------------------------");
- System.out.println(" // ELIJA UNA OPCIÓN DEL MENÚ//");
- System.out.println("1.Ingresar números al arreglo ");
- System.out.println("2.Generar números aleatoriamente ");
- System.out.println("3.Salir");
- opcion=ReadInt(sc,message);
- if(opcion<0 || opcion>3){
- System.out.println("\nError: Ingrese una opción valida");
- }
- }while(opcion<0 || opcion>3);
- return opcion;
- }
- public static int ReadInt(Scanner sc, String message) {
- int num;
- String line;
- while (true) {
- try {
- System.out.print(message);
- line = sc.nextLine();
- num = Integer.parseInt(line);
- break;
- }
- catch (Exception e) {
- System.out.println("\nERROR al cargar un número entero");
- }
- }
- return num;
- }
- //Valida que el tamaño del vector sea positivo y entero
- public static int PositivoInt(Scanner sc, String message)
- {
- int num;
- while(true) {
- num=ReadInt(sc,message);
- if (num>0) {
- break;
- }
- else {
- System.out.println("El valor de dimensión del vector debe ser positivo y entero");
- }
- }
- return num;
- }
- public static void cargarVector(Scanner sc, int V[])
- {
- System.out.println("Cargando vector de números múltiplos de 7 pero no de 3");
- for (int i=0; i < V.length; i++ )
- {
- int num=ReadInt(sc,"Ingrese un número entero: ");
- if ( Multiplo7(num) && !Multiplo3(num))
- {
- V[i]=num;
- }else {
- i=i-1;
- }
- }
- }
- public static void VectorAleatorio( int V[])
- {
- Random rnd=new Random();
- for (int i =0; i < V.length; i++ )
- {
- int num=rnd.nextInt(100)+1;
- if ( Multiplo7(num) && !Multiplo3(num))
- {
- V[i]=num;
- }else {
- i=i-1;
- }
- }
- }
- public static boolean Multiplo7(int num)
- {
- boolean multiplo=false;
- if (num % 7 ==0) {
- multiplo=true;
- }
- return multiplo;
- }
- public static boolean Multiplo3(int num)
- {
- boolean multiplo=false;
- if (num % 3 ==0) {
- multiplo=true;
- }
- return multiplo;
- }
- public static void Mostrar(int V[])
- {
- System.out.println("\nVector generado");
- for (int i=0; i < V.length; i++ )
- {
- System.out.println("Posicion"+ (i+1)+ ": " + V[i]);
- }
- }
- public static void MostrarInvertido(int V[])
- {
- System.out.println("\nVector invertido");
- for (int i=V.length-1; i >= 0; i-- )
- {
- System.out.println("Posicion"+ (i+1)+ ": " + V[i]);
- }
- }
- public static void Pares(int V[])
- {
- int contPar=0;
- for (int i=0; i < V.length; i++ )
- {
- if (V[i] % 2 ==0)
- contPar=contPar +1;
- }
- System.out.println("\nCantidad de números pares ingresados:" + contPar);
- System.out.println("\nCantidad de números impares ingresados:" + (V.length - contPar));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement