• Lenguaje

    Java usando Scanner

  • Descripción

    En una tienda se ha registrado N clientes durante el mes, llenaron una ficha de atención con los siguientes datos Edad y Sexo (1-Masculino, 2-Femenino) de cada cliente atendido.
    Determinar la cantidad de hombres y mujeres ha ingresado a la tienda durante el mes, también determinar el porcentaje de hombres y mujeres atendidos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;

public class RegistroDeClientes {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int hombres, i, mujeres, n, sexo;
        double porcentaje_hombres, porcentaje_mujeres;
        String edad;
        hombres = 0;
        mujeres = 0;
        porcentaje_hombres = 0;
        porcentaje_mujeres = 0;
        System.out.print("Ingresa el valor de n: ");
        n = in.nextInt();
        in.nextLine();
        for (i=1; i<=n; i++) {
            System.out.print("PROCESO " + i);
            System.out.print("Ingresa el edad: ");
            edad = in.nextLine();
            System.out.println("Selecciona el valor de sexo.");
            System.out.println("\t1.- Masculino");
            System.out.println("\t2.- Femenino");
            System.out.print("\t: ");
            do {
                sexo = in.nextInt();
                in.nextLine();
                if (sexo<1||sexo>2)
                    System.out.print("Valor incorrecto. Ingr\u00E9salo nuevamente.: ");
            } while (sexo<1||sexo>2);
            if(sexo==1)
                hombres++;
            else
                mujeres++;
            System.out.println("Edad: " + edad);
            System.out.println();
        }
        if (n == 0) {
            porcentaje_hombres = 0;
        } else {
            porcentaje_hombres=100.0*hombres/n;
        }
        if (n == 0) {
            porcentaje_mujeres = 0;
        } else {
            porcentaje_mujeres=100.0*mujeres/n;
        }
        System.out.println("Valor de hombres: " + hombres);
        System.out.println("Valor de mujeres: " + mujeres);
        System.out.println("Valor de porcentaje hombres: " + porcentaje_hombres);
        System.out.println("Valor de porcentaje mujeres: " + porcentaje_mujeres);
    }

}