I am doing a class exercise, I put the statement and I explain.
Make a program that allows us to accept numbers greater than or equal to zero by keyboard until a negative number is entered. At that time, the following menu will be displayed on the screen:
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 to “Exit”.
In the first step I don't know if I have to accumulate the numbers entered, I have put it that way, even though I still have it wrong, I have saved them in a array
for the menu. The thing is that I'm trying to do the switch
with the menu, but I don't know how to do those operations, if they go inside the case or outside. My second doubt is how to visualize the options for the user before clicking on one, see what he does previously. I leave you 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. ");
}
}
}
I'll give you an example of more or less (sorry for the errors of indentation or semicolons that I have done in the editor this head like this. I can give you an example putting the menu in a separate class so that there is not so much code left but you can do it right there, in the end it is the same. The switch inside each case you have to create the logic that you have chosen in the menu. You pass the option to it and depending on what you have entered it will do one thing or another. A good practice is include the default in the switch, which I have not done but you can investigate about it. I hope it has helped you, if you have any questions, you already know, we are here!
Let's take the Oracle Switch tutorial as a basis , we have that Switch is used in such a way that we have
switch (opcion)
and depending on the value ofopcion
enters acase
if there is no onecase con el valor
is entereddefault
(if it is defined)}
for this you must use Methods , Methods link 2 Methods are blocks of code that allow you to have Reusable functionalities. therefore in an example:
note: the code provided has syntax or logic errors:
opcion
it never changes value. always is0
System.out.print("La suma de los números pares introducidos son" +(lista%2==0) +lista);
list is not a "number" so you can't perform an operation on the list object(lista%2==0)
and+lista
it may not give the useful result to be displayed.Regarding case 5, you could ask at the beginning if it is 0. If it is NOT, then you 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.
This way, the only way it will ask if it's even is if it isn't 0, so it won't count as even.
Hope this can help you!!
PS: I don't understand how the "zero" variable works.
PD2: Sorry for the indentation, it's the first time I answer and I don't know how to do it =).