Menu with different executions in java

1

I'm doing a class exercise, I'll give you the statement and I'll explain.

Perform a program that allows us to accept numbers greater than or equal to zero per keyboard until a negative number is entered. At that moment the following menu will be displayed:

1-Suma de los números pares introducidos.
2-Media de los números pares introducidos. (con dos decimales)
3-Mayor nº impar introducido.
4-Cuántos números hemos introducido.
5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.
6.-Salir.

The menu (with the values already entered) will be repeated until the user decides "Exit".

In the first step I do not know if I have to accumulate the entered numbers, I have put it that way, even though I have it wrong, I have saved them in a array for the menu. The thing is that I am trying to do the switch with the menu, but I do not know how to do those operations, whether they are inside the case or outside. My second question is how to visualize the options for which user before giving in any, see what you do previously. I leave what I have done.

public class Ejercicio4 {

    public static void main(String[] args) {
        System.out.println("Progama que nos permite aceptar numeros mayores o iguales a cero y si no, aparecerá un menú.");
        System.out.println("----------------------------------------------------------------------------------------------------------------------------------------\n");
        Scanner teclado;
        int numero;
        List<Integer> lista = new ArrayList<Integer>();

        int salir = 6;
        int opcion = 0;
        try {

            do {
                System.out.print("Introduce un número: ");
                teclado = new Scanner(System.in);
                numero = teclado.nextInt();
                if (numero >= 0) {

                    lista.add(numero);
                }

            } while (numero >= 0);
            if (numero < 0) {
                while (opcion != salir) {
                    System.out.println("--------- Menú de opciones --------");
                    System.out.println("1-Suma de los números pares introducidos ");
                    System.out.println("2-Media de los números pares introducidos. (con dos decimales) ");
                    System.out.println("3-Mayor nº impar introducido. ");
                    System.out.println("4-Cuántos números hemos introducido. ");
                    System.out.println("5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares ");
                    System.out.println("6-Salir ");

                    teclado = new Scanner(System.in);
                    opcion = teclado.nextInt();
                    switch (opcion) {
                        //1-Suma de los números pares introducidos
                        case 1:

                            int suma = 0;
                            for (int i = 0; i < lista.size(); i++) {

                                int num = lista.get(i);
                                if (num % 2 == 0) {
                                    suma = num + suma;
                                }
                            }
                            System.out.println("La suma de los números pares introducidos son" + suma);
                            break;

                        //2-Media de los números pares introducidos. (con dos decimales)
                        case 2:
                            double media = 0;
                            int x = 0;
                            suma = 0;
                            for (int i = 0; i < lista.size(); i++) {
                                int num = lista.get(i);

                                if (num % 2 == 0) {
                                    suma = num + suma;
                                    x++;
                                }

                            }
                            media = suma / x;
                            System.out.println("La media de los números pares introducidos son " + Math.round(media * 100d) / 100d + "\n");

                            break;

                        //3-Mayor nº impar introducido.
                        case 3:
                            suma = 0;
                            int max = 0;
                            for (int i = 0; i < lista.size(); i++) {

                                int num = lista.get(i);
                                if (!(num % 2 == 0)) {
                                    if (num > max) {
                                        max = num;
                                    }
                                }
                            }
                            System.out.println("La media de los números pares introducidos son " + max);
                            break;
                        //4-Cuántos números hemos introducido.
                        case 4:
                            int numeros = 0;
                            for (int i = 0; i < lista.size(); i++) {

                                numeros++;
                            }
                            System.out.println("Cuantos números hemos introducido " + numeros);

                            break;
                        //5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.
                        case 5:

                            int pares = 0;
                            int impares = 0;
                            int ceros =0;
                            int cero=0;
                            for (int i = 0; i < lista.size(); i++) {

                                int num = lista.get(i);
                                if (num % 2 == 0) {
                                    pares++;
                                }else if(!(num % 2 == 0)&& num!=0){
                                impares++;
                                } 
                                 if (num==0){
                                ceros++;
                                } 
                                }
                            System.out.println("Los números pares han sido : " + pares);
                            System.out.println("Los números impares han sido : " + impares);
                            System.out.println("Los números cero han sido : " + ceros);
                            break;
                        //6.-Salir.
                        case 6:

                            break;
                    }
                }

            }

        } catch (InputMismatchException ime) {
            System.out.println("¡Cuidado! Solo puedes insertar números. ");

        }

    }

}'introducir el código aquí'
        public static void main(String[] args) {
            System.out.println("Progama que nos permite aceptar numeros mayores o iguales a cero y si no, aparecerá un menú.");
            System.out.println("----------------------------------------------------------------------------------------------------------------------------------------\n");
            Scanner teclado;
            int numero;
            List lista = new ArrayList();

            int salir = 6;
            int opcion = 0;
            try {
                System.out.print("Introduce un número: ");
                teclado = new Scanner(System.in);
                numero = teclado.nextInt();
                do {

                    lista.add(numero);
                } while (numero >= 0);
                if (numero < 0) {
                    while (opcion != salir) {
                        System.out.print("Elige una opción: ");

                        switch (opcion) {
                            //1-Suma de los números pares introducidos
                            case 1: 
                            System.out.print("La suma de los números pares introducidos son" +(lista%2==0) +lista);
                            break;

                            //2-Media de los números pares introducidos. (con dos decimales)
                            case 2: break;  
                            //3-Mayor nº impar introducido.
                            case 3: break;    
                            //4-Cuántos números hemos introducido.
                            case 4: break;
                            //5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.
                            case 5: break;   
                            //6.-Salir.
                            case 6: break;
                        }
                    }

                }
            } catch (InputMismatchException ime) {
                System.out.println("¡Cuidado! Solo puedes insertar números. ");

            }

        }

    }
    
asked by Flowcou 17.12.2018 в 20:25
source

3 answers

0

With regard to case 5, you could ask at the beginning if it is 0. In case it is NOT, just ask if it is even, and then if it is odd. In this way, if it is 0 it will not count as a pair.

case 5:

int pares = 0;
int impares = 0;
int ceros = 0;
int cero = 0;
for (int i = 0; i < lista.size(); i++) {
    int num = lista.get(i);
    if (num == 0) {
        ceros++;
    } else if (num % 2 == 0) {
        pares++;
    } else {
        impares++;
    } 
}
System.out.println("Los números pares han sido : " + pares);
System.out.println("Los números impares han sido : " + impares);
System.out.println("Los números cero han sido : " + ceros);
break;

In this way, the only way in which you ask if it is even is that it is not 0, so it will not count as a pair.

I hope it serves you!

PS: I do not understand the operation of the variable "zero".

PD2: Sorry for the identacion, it is the first time I answer and I do not know how it is done =).

    
answered by 18.12.2018 / 23:37
source
0

I'll give you an example of more or less (apology for the errors of indentation or semicolons that I have done in the editor this way.) I've given you an example by putting the menu in a separate class so that there is no So much code, but you can do it right there, in the end it's the same, the switch inside each case you have to create the logic that you have chosen in the menu, you pass the option and it depends on what you have introduced one thing or another. good practice is to include the default in the switch, which I have not done but you can investigate about it I hope it has helped you, any doubt you know, here we are!

  public class Ejercicio4 {
    Scanner teclado = new Scanner(System.in);
        public static void main(String[] args) {
            System.out.println("Progama que nos permite aceptar numeros mayores o iguales a cero y si no, aparecerá un menú.");
            System.out.println("----------------------------------------------------------------------------------------------------------------------------------------\n");
        int numero=0;
        int opcion = 0;
        List lista = new ArrayList();
        try {

           do {
                System.out.print("Introduce un número: ");
                numero = teclado.nextInt();
                lista.add(numero);
            } while (numero >= 0);

            do{
            opcion = menu()
             switch (opcion) {
                        //1-Suma de los números pares introducidos
                        case 1: 
                         //Logica del caso 1
                        System.out.print("La suma de los números pares introducidos son" +(lista%2==0) +lista);
                        break;

                        //2-Media de los números pares introducidos. (con dos decimales)
                        case 2: //Logica del caso 2 break;  
                        //3-Mayor nº impar introducido.
                        case 3: //Logica del caso 3 break;    
                        //4-Cuántos números hemos introducido.
                        case 4: break;
                        //5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.
                        case 5: break;   
                        //6.-Salir.
                        case 6:
                         System.out.print("Hasta la vista baby!");
                        break;
                    }
            }while (opcion != 6); 


        } catch (InputMismatchException ime) {
            System.out.println("¡Cuidado! Solo puedes insertar números. ");

        }

    }

}

public static Integer menu(){

Integer op;

System.out.println("Menu");
System.out.println("1.Sumar");
System.out.println("2.blabla");
System.out.println("3.Salir");
System.out.println("elige una opcion:");
op = teclado.nextInt();

return op;
}
    
answered by 18.12.2018 в 00:44
0
  

The thing is that I'm trying to make the switch with the menu, but I do not know how to do those operations, whether they go inside the case or outside.

let's take as a base the Oracle Switch tutorial we have that Switch is used to form that we have switch (opcion) and depending on the value of opcion enter a case if there is no case con el valor you enter default (if defined)

switch (opcion) {
    case 1: 
        //codigo a Ejecutar si se da el Case 1
        //Suma de los números pares introducidos
        //NOTA el Break se utiliza para salir del switch.
        break;
    case 2:
        //codigo a Ejecutar si se da el Case 2
        //2-Media de los números pares introducidos. (con dos decimales)
        break;  
     case 3: 
        //codigo a Ejecutar si se da el Case 3
        //3-Mayor nº impar introducido.
        break;
     case 4:
        //codigo a Ejecutar si se da el Case 
        //4-Cuántos números hemos introducido.             
        break;            
     case 5:
        //5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.
        break;
     case 6: 
        //6.-Salir.
        break;
     default:
         //codigo a Ejecutar si el valor no esta en case 
        // que hacer si opcion no es ninguno de los valores del case. 
        break;

}

  

My second question is how to visualize the options for which user   before giving in any, see what you do previously.

for this you should use Methods , Methods link 2 The methods are code blocks that allow to have reusable functionalities. so in an example:

//esto es una constante por tanto definida como final
private static final int SALIR = 6;

public static void main(String[] args) {
    System.out.println("Progama que nos permite aceptar numeros mayores o iguales a cero y si no, aparecerá un menú.");
    System.out.println("----------------------------------------------------------------------------------------------------------------------------------------");
    System.out.println();
    Scanner teclado = new Scanner(System.in);
    ArrayList<Integer> lista = leerNumeros(teclado);
    Acciones(teclado, lista);
}

private static void mostrarMenu() {
    System.out.println("------------------------------------------------------------------------------------------------");
    System.out.println("Menu:");
    System.out.println();
    System.out.println("1-Suma de los números pares introducidos.");
    System.out.println("2-Media de los números pares introducidos. (con dos decimales)");
    System.out.println("3-Mayor nº impar introducido.");
    System.out.println("4-Cuántos números hemos introducido.");
    System.out.println("5-Cuantos números de los introducidos han sido ceros, cuántos han sido pares y cuantos impares.");
    System.out.println("6.-Salir.");
    System.out.println("------------------------------------------------------------------------------------------------");
    System.out.println();
}

private static void Acciones(Scanner teclado, ArrayList lista) {
    int opcion;
    do {
        mostrarMenu();
        opcion = teclado.nextInt();
        //...
        //switch
        //...
    } while (opcion != SALIR);
}

private static ArrayList<Integer> leerNumeros(Scanner teclado) {
    System.out.println("------------------------------------------------------------------------------------------------");
    System.out.println();
    System.out.println("Ingreso de numeros, para terminar el ingreso de numeros ingrese un numero negativo");
    System.out.println();
    ArrayList<Integer> valores = new ArrayList<>();
    int numero;
    do {
        System.out.print("Introduce un número: ");
        try {
            numero = teclado.nextInt();
            if (numero >= 0) {
                valores.add(numero);
            }
        } catch (InputMismatchException err) {
            numero=0;
            teclado.next();//descarta el valor que "no es valido"
            teclado.reset();
            System.out.println("Valor Invalido, Si desea salir Ingrese un valor negativo.");
        }
    } while (numero >= 0);
    return valores;
}

note: the code provided has syntax or logic errors:

opcion never changes value. it is always 0

System.out.print("La suma de los números pares introducidos son" +(lista%2==0) +lista); the list is not a "number" so you can not perform an operation on the list object (lista%2==0) and also +lista may not give the useful result to be displayed.

    
answered by 18.12.2018 в 02:50