Average input data algorithm

Asked

Viewed 381 times

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


    }
}
  1. A survey was made 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 shows the following information:

    a) mean age of the group; OK*

    b) average height of women; OK*

    c) average age of men; OK*

    d) percentage of persons aged between 18 and 35 (including)

I’m not getting the average age and height. Letter "d" too.

I put three in the repeating frame just to make it easier.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

3

There are several problems in the code and this is mainly caused because it is a complete mess. I only understood after rewriting everything:

import java.util.Scanner;

public class Main {
    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));
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I simplified some things that could be simpler, between them leave the 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’s okay, ended up leaving neutral if it’s something invalid.

I don’t even know how to explain everything I’ve done that’s so different, it just came out. I remember that had the average calculation occurred within the loop, which makes no sense, can only calculate it after having all information. There were conceptual errors adding up things that shouldn’t, so much of the problem is mathematical.

2

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

So instead of:

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

You must:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.