Average data entered algorithm

0
package ifal2;
import java.util.Scanner;

public class Lista3Questao4 {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);


        int idade=0, contM=0, contF=0;
        double soma=0, somaaltura=0, somaM=0, percentual=0, altura;
        String sexo;

        for(int x=0; x < 3; x++) {
        System.out.println("Digite o sexo: ");
        sexo = entrada.next();
        System.out.println("Digite a idade: ");
        idade = entrada.nextInt();
        System.out.println("Digite a altura: ");
        altura = entrada.nextDouble();

        soma = soma + idade;

        if (sexo == "1") {
        somaM = somaM + idade;
        contM++;
        }
                        if (sexo == "0") {
                        somaaltura = somaaltura + altura;
                        contF++;
                        }

                somaM = somaM/contM;
                somaaltura = somaaltura/contF;

                        if (idade >= 18 && idade <= 35) {
                                percentual = percentual + idade;
                                }

        }
        System.out.println("Média da idade (geral) " + (soma/3));
        System.out.println("Média da altura (feminino) " + (somaaltura));
        System.out.println("Média da idade (masculino) " + (somaM));
        System.out.println("Percentual de pessoas com idade entre 18-35 anos: " + (percentual/3));


    }
}
  
  • A survey was carried out among the 1000 inhabitants of a region to collect   the following data:   sex (0-female, 1-male), age and height.   make an algorithm that reads the collected information and show the following information:

         

    a) mean age of the group; OK *

         

    b) mean height of women; OK *

         

    c) mean age of men; OK *

         

    d) percentage of people aged between 18 and 35 years (inclusive)

  •   

    I'm not getting the average age and height. Letra "d" also.

    I put 3 in the repeat structure just to make it easier.

        
    asked by anonymous 01.08.2018 / 04:28

    2 answers

    3

    It has several problems in the code and this is mainly caused because it is a complete mess. I just understood after rewriting everything:

    import java.util.Scanner;
    
    public class Program {
        public static void main(String[] args) {
            Scanner entrada = new Scanner(System.in);
            int soma = 0, somaM = 0, somaF = 0, contM = 0, contF = 0, contJ = 0;
            for (int x = 0; x < 3; x++) {
                System.out.println("Digite o sexo: ");
                int sexo = entrada.nextInt();
                System.out.println("Digite a idade: ");
                int idade = entrada.nextInt();
                System.out.println("Digite a altura: ");
                int altura = entrada.nextInt();
                soma += idade;
                if (sexo == 1) {
                    somaM += idade;
                    contM++;
                } else if (sexo == 0) {
                    somaF += altura;
                    contF++;
                }
                if (idade >= 18 && idade <= 35) contJ++;
            }
            System.out.println("Média da idade (geral) " + ((double)soma / 3));
            System.out.println("Média da altura (feminino) " + ((double)somaF / contF));
            System.out.println("Média da idade (masculino) " + ((double)somaM / contM));
            System.out.println("Percentual de pessoas com idade entre 18-35 anos: " + (100 / 3 * (double)contJ));
        }
    }
    

    See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    I simplified some things that could be simpler, among them to leave sex as numerical since it is not using the commonly used letters, and I preferred to use height as centimeters. I could have given better names, tested the sex to see if it is ok, I ended up leaving neutral if it is something invalid.

    I do not even know how to explain everything I've done so different that I ended up leaving. I remember that had the average calculation occurred inside the loop, which does not make sense, you can only calculate it after having all information. I had conceptual errors adding things that should not, well, much of the problem is mathematical.

        
    01.08.2018 / 05:02
    2

    You should count how many people are between 18 and 35, and not add up to the ages of those who satisfy the condition.

    So instead of:

    if (idade >= 18 && idade <= 35) {
        percentual = percentual + idade;
    }
    

    You should:

    /* Inicializando variáveis */
    int pessoasRangeIdade = 0;
    
    /* Dentro do for */
    if (idade >= 18 && idade <= 35) {
        pessoasRangeIdade++;
    }
    
    /* Após o for */
    percentual = pessoasRangeIdade/3; //Sendo 3 a quantidade total de pessoas - Sugiro deixar dinâmico na entrada do programa.
    
    System.out.println("Percentual de pessoas com idade entre 18-35 anos: " + (percentual));
    
        
    01.08.2018 / 04:47