Doubt with average in java [duplicated]

0

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);


}

}

    
asked by Kevin Marshall 03.02.2018 в 21:59
source

2 answers

0

for what you ask you only have to have the float average and total, I show you my code so you can see how I did it:

package promedio;

public class Promedio {

    public static void main(String[] args) {
        int edades[] = {7, 8, 7, 9, 6};
        int edad = 0;
        int contador = 0;
        float total = 0;
        float promedio = 0;

        while (contador < 5) {
            edad = edades[contador];
            contador++;
            total = (float) total + edad;
            promedio = total / 5;
        }
        System.out.println("Total: " + total);
        System.out.println("Promedio: " + promedio);
    }
}
    
answered by 04.02.2018 в 03:47
0

Look at the problem with your code consists of this:

Even if your variable average1 is declared as float , since your total and age_man are of type < em> int , you are discarding the fractional part of the quotient before assigning the result of the division to average1 . Since dividing two integer variables produces an integer result.

To perform a floating-point calculation with integer values, you must temporarily treat these values as floating point numbers for use in the calculation, that is, you need to add a unary type conversion operator .

What iuninefrendor replied to you is correct, however, it does not explain why this happens or why it should be done like this. As shown in the code you wrote, the part you should correct is this:

average1 = (float) total / 5;

The calculation now consists of a floating-point value (the temporary float copy of total) divided by the integer 5.

Adding the (float) creates a temporary floating-point copy of the total operand (which appears to the right of the operator). Using a type conversion operator in this way is a process that is called explicit conversion or type conversion . However, the value stored in the total variable is still an integer.

    
answered by 04.02.2018 в 11:32