How do I do this program? [closed]

0

Enter a group of 20 people and only count those who are between 18 and 45 years old.

The problem is that I do not know how to put the condition of counting ages between 18 and 45.

    int personas = 21;
    int edadDe18A45 = 0;
    int edades = 0;
    Scanner teclado = new Scanner(System.in);

    for (int i = 1; i < personas; i++) {

        System.out.println(i + "Ingrese edad : ");
        edades = teclado.nextInt();

        if (17 < edades && edades > 44) { // No se si esta bien como puse la condicion en el if :/

            // Al poner esa condicion me cuenta edades de 18 hasta el 90 y mas...

            edadDe18A45++;
        }

    }

    System.out.println("Cantidad de personas con edad comprendida entre 18 y 45 años : " + edadDe18A45);
    
asked by computer96 28.08.2018 в 21:49
source

2 answers

1

Friend the only fault you have is in the conditionals, I recommend that you adjust your conditionals in such a way that when you read them you can easily understand them:

public class Main
{
    public static void main(String[] args) {
        int personas = 20;
        int edadDe18A45 = 0;
        int edades = 0;
        java.util.Scanner teclado = new java.util.Scanner(System.in);
        for (int i = 1; i <= personas; i++) {
            System.out.println(i + "Ingrese edad : ");
            edades = teclado.nextInt();
            if (edades >= 18 && edades <= 45) {
                edadDe18A45++;
            }
        }
        System.out.println("Cantidad de personas con edad comprendida entre 18 y 45 años : " + edadDe18A45);
    }
}
    
answered by 28.08.2018 / 22:48
source
0

Think of a line of natural numbers. The numbers start in the following order: 1,2,3,4,5 ... and so on to infinity and beyond.

Therefore, if you want to make the age range of 18 to 45 we should have the following order according to the mathematics: 18

answered by 28.08.2018 в 22:39