• Lenguaje

    Java usando Scanner

  • Descripción

    Leídos el nombre, sexo y edad de varias personas se desea saber:
    a) Cantidad de mujeres
    b) Porcentaje de hombres
    c) Número de personas con edad menor o igual a 20 años.

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
55
import java.util.Scanner;

public class CantidadYPorcentajeDePersonas {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a, c, edad, n, sexo;
        double b;
        String nombre;
        String tecla_repetir;
        a = 0;
        c = 0;
        n = 0;
        b = 0;
        do {
            System.out.print("Ingresa el nombre: ");
            nombre = in.nextLine();
            System.out.print("Ingresa el valor de edad: ");
            edad = in.nextInt();
            in.nextLine();
            in.nextLine();
            System.out.println("Selecciona el valor de sexo.");
            System.out.println("\t1.- Mujer");
            System.out.println("\t2.- Hombre");
            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);
            n=n+1;
            if(sexo==1)
                a=a+1;
            if(edad<=20)
                c=c+1;
            System.out.println("Nombre: " + nombre);
            System.out.println();
            do {
                System.out.print("\u00BFDeseas repetir el proceso? (S/N): ");
                tecla_repetir = in.nextLine();
            } while (!tecla_repetir.equalsIgnoreCase("s") && !tecla_repetir.equalsIgnoreCase("n"));
        } while (tecla_repetir.equalsIgnoreCase("s"));
        if (n == 0) {
            b = 0;
        } else {
            b=100.0*(n-a)/n;
        }
        System.out.println("Valor de a: " + a);
        System.out.println("Valor de c: " + c);
        System.out.println("Valor de n: " + n);
        System.out.println("Valor de b: " + b);
    }

}