Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Punto4 {
- public static void main(String[] args) {
- Scanner consola = new Scanner(System.in);
- ArrayList<Integer> numero = new ArrayList<Integer>();
- int opc = -1,suma;
- double promedio;
- while (opc != 3) {
- menu();
- opc=Modulo2.validarNumero(consola, "Ingrese una opcion:");
- switch(opc) {
- case 1:
- numero=cargaVector(numero, consola);
- mostrarArray(numero);
- sumaPar(numero);
- productoImpar(numero);
- suma=sumapromedio(numero);
- promedio=suma/numero.size();
- System.out.println("El promedio calculado fue: "+promedio);
- contar(numero, promedio);
- break;
- case 2:
- numero=cargaRandom(numero);
- mostrarArray(numero);
- sumaPar(numero);
- productoImpar(numero);
- suma=sumapromedio(numero);
- promedio=suma/numero.size();
- System.out.println("El promedio calculado fue: "+promedio);
- contar(numero, promedio);
- break;
- case 3:
- System.out.println("Cerrando programa..." );
- break;
- }
- }
- }
- public static void menu() {
- System.out.println("***Menu Principal***");
- System.out.println("1 - Ingreso de valores manualmente.");
- System.out.println("2 - Generar valores random.");
- System.out.println("3 - Salir.");
- }
- public static ArrayList<Integer> cargaVector(ArrayList numeros,Scanner ingreso) {
- Scanner sc= new Scanner(System.in);
- int carga;
- String resp = "si" ;
- while(resp.equalsIgnoreCase("Si"))
- { carga = Modulo2.validarNumero(ingreso, "Ingrese un numero:");
- numeros.add(carga);
- System.out.println("Desea continuar S/N. ");
- resp=sc.next();
- }
- return numeros;
- }
- public static void mostrarArray(ArrayList numeros) {
- for (int i=0;i<numeros.size();i++) {
- System.out.println(numeros.get(i));
- }
- }
- public static void productoImpar(ArrayList numeros) {
- int producto=1;
- for (int i=0;i<numeros.size();i++) {
- if ((int) numeros.get(i) % 2 != 0) {
- producto=(int) numeros.get(i)*producto;
- }
- }
- if (producto==1) {
- System.out.println("El producto de los numeros impares es: "+ 0);
- }
- System.out.println("El producto de los numeros impares es: "+ producto);
- }
- public static void sumaPar(ArrayList numeros) {
- int suma=0;
- for (int i=0;i<numeros.size();i++) {
- if ((int) numeros.get(i) % 2 == 0) {
- suma=(int) numeros.get(i)+suma;
- }
- }
- System.out.println("La suma total de los numeros pares es: "+ suma);
- }
- public static int sumapromedio(ArrayList numeros) {
- int total=0;
- for (int i=0;i<numeros.size();i++) {
- total=total+(int) numeros.get(i);
- }
- return total;
- }
- public static void contar (ArrayList numeros, double promedio) {
- int contador=0;
- for (int i=0;i<numeros.size();i++) {
- if ((int) numeros.get(i) > promedio) {
- contador=contador + 1;
- }
- }
- System.out.println("Cantidad de valores mayor al promedio: " + contador);
- }
- public static ArrayList<Integer> cargaRandom(ArrayList numeros) {
- Scanner sc= new Scanner(System.in);
- int carga;
- String resp = "si" ;
- while(resp.equalsIgnoreCase("Si"))
- { carga = (int) (Math.random()*100);
- System.out.println("Valor generado por random: "+ carga);
- numeros.add(carga);
- System.out.println("Desea continuar S/N. ");
- resp=sc.next();
- }
- return numeros;
- }
- }
Add Comment
Please, Sign In to add comment