Good to everyone, I have a little doubt, when making the average age (in this case are 5 people who enter their ages) and the operation is done:
promedio de edad = suma total de edades / 5
I want to go 'at least' with a decimal, I mean:
average age = (28 + 24 + 25 + 33 + 37) / 5 = 29.4
However, for more than the variable 'average age' I have declared it as float, it always appears this way: 29.0
I leave my code here so that you have a better overview of my problem: (
package trabajo_lab2_prom_edad;
import java.util.Scanner;
public class Trabajo_Lab2_Prom_Edad {
public static void main(String[] args) {
int edad_hombre = 0;
int edad_mujer = 0;
float promedio1 = 0;
float promedio2 = 0;
int contenedor1 = 0;
int contenedor2 = 0;
int total = 0;
Scanner leer = new Scanner(System.in);
int contador_hombre = 1;
while (contador_hombre <= 5) {
System.out.println("Ingrese la edad del " + contador_hombre + " hombre ");
edad_hombre = leer.nextInt();
leer.nextLine();
contador_hombre++;
total = total + edad_hombre;
promedio1 =total/5;
}
System.out.println("suma total de edades hombre: " + total);
System.out.println("El promedio de edad de hombres es de: " + promedio1 + "%" );
//-------------------------------------------------------------------------//
int contador_mujer = 1;
while(contador_mujer <=5){
System.out.println("----------------------------------------------------------");
System.out.println("Ingrese la edad de la " + contador_mujer + " mujer ");
edad_mujer = leer.nextInt();
leer.nextLine();
contador_mujer++;
total= total + edad_mujer;
promedio2 = total/5;
}
System.out.println("suma total de edades mujer: " + total);
System.out.println("El promedio de edad de mujeres es de: " + promedio2 + "%");
leer.close();
System.out.println("----------------------------------------------------------");
System.out.println("Suma de promedio de edades entre hombres y mujeres: ");
double totalprom;
totalprom = promedio1 + promedio2;
System.out.println(totalprom);
System.out.println("Redondeo de ambos promedios: ");
int i2 = (int) totalprom;
System.out.println(totalprom);
}
}