how can I make an array with that information

1

In a census we enter Weights of n people in any order is required to find • How many are thin (0 to 70), fat (71 to 90) and very fat ((> 90).

    
asked by gmatsy 05.07.2018 в 02:25
source

2 answers

-1

If I understand your question, you can implement something like this and use an ArrayList since you will not know how many people will be counted, then just go through the ArrayList and check the weights and save them as in a counter that correspond to the criteria that you mention.

public static void main(String arrs[]){

         Scanner entrada = new Scanner (System.in);

            int delgados=0;
            int gordos=0;
            int obesos=0;
            Double peso=0.0;
            int aux;
            ArrayList<Double> pesos = new ArrayList<Double>();
            System.out.println("***Censo***");  
            do{

                System.out.println("Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)");
                peso = entrada.nextDouble();
                if(peso>0){
                    pesos.add(peso);
                }


            } while(peso>0);


            for(aux = 0; aux<pesos.size(); aux++){
                peso = pesos.get(aux);
                if(peso<=70){
                    delgados++;
                }else if(peso<=90){
                    gordos++;
                }else{
                    obesos++;
                }
            }
            System.out.println("***Resultados***");
            System.out.println("Delgados: "+delgados);
            System.out.println("Gordos: "+gordos);
            System.out.println("Obesos: "+obesos);
    }
    
answered by 05.07.2018 в 03:11
-1

First, I would like to suggest that you offer a Minimum, complete and verifiable example on all questions, as you must show a minimum effort in solving your problem.

Having said that, I would like to respond in the same way and if you are learning POO it would be good to divide your problem into classes (divide and conquer!). I implemented a solution for your problem.

Person:

This class instantiates a person object, which has a weight attribute.

public class Persona {
    Double peso;

    public Persona(Double peso) {
        this.peso = peso;
    }

    public Double getPeso() {
        return peso;
    }

    public void setPeso(Double peso) {
        this.peso = peso;
    }

    @Override
    public String toString() {
        return "Persona [peso=" + peso + "]";
    }


}

Census:

This is the census class, which contains an object census, which houses an Arraylist of people to be processed under its parameters (groups of people by weight). In addition, overwrites the toString method to print the census data in the main program.

Because it is the census that divides people by their weight, it is this that filters people into the methods getDelgados , getGordos , GetMuyGordos using streams

import java.util.ArrayList;
import java.util.stream.Collectors;

public class Censo {
    private ArrayList<Persona> personas = new ArrayList<Persona>();

    public Censo() {
        super();
    }

    public Censo(ArrayList<Persona> personas) {
        super();
        this.personas = personas;
    }

    public ArrayList<Persona> getPersonas() {
        return personas;
    }

    public void setPersonas(ArrayList<Persona> personas) {
        this.personas = personas;
    }

    public void addPersona(Persona p) {
        this.personas.add(p);
    }

    public ArrayList<Persona> getDelgados(){
        return (ArrayList<Persona>) personas.stream().filter(p -> p.getPeso() <= 70).collect(Collectors.toList());
    }

    public ArrayList<Persona> getGordos(){
        return (ArrayList<Persona>) personas.stream().filter(p -> p.getPeso() >= 71 && p.getPeso() <= 90).collect(Collectors.toList());
    }

    public ArrayList<Persona> getMuyGordos(){
        return (ArrayList<Persona>) personas.stream().filter(p -> p.getPeso() > 90).collect(Collectors.toList());
    }

    @Override
    public String toString() {
        return "Censo [personas=" + personas + "]";
    }

}

MainProgram:

Finally, we use our classes to our advantage. Allowing the user to create people for the census through Scanner and filters for validate our income (ends in zero and does not allow negative weights)

import java.util.Scanner;

public class MainProgram {

    public static void main(String[] args) {
        Censo censo = new Censo();
        Scanner sc = new Scanner(System.in);
        Double peso;

        do {
            Double temp;
            System.out.println("Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)");
            while (!sc.hasNextDouble() || (temp = sc.nextDouble()) < 0) {
                System.out.println("Error");
                System.out.println("Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)");
            }
            peso = temp;
            sc.nextLine();
            censo.addPersona(new Persona(peso));
        } while (peso != 0);

        System.out.println("Delgados: ");
        censo.getDelgados().forEach(e -> System.out.println(e));

        System.out.println("Gordos: ");
        censo.getGordos().forEach(e -> System.out.println(e));

        System.out.println("Muy Gordos: ");
        censo.getMuyGordos().forEach(e -> System.out.println(e));

    }

}

Example:

A small example of the execution of the program and its outputs.

Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
5
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
10
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
70
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
71
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
90
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
91
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
85
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
120
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
-3
Error
Ingresa el peso de la persona (presiona 0 para salir y ver los resultados)
0
Delgados: 
Persona [peso=5.0]
Persona [peso=10.0]
Persona [peso=70.0]
Persona [peso=0.0]
Gordos: 
Persona [peso=71.0]
Persona [peso=90.0]
Persona [peso=85.0]
Muy Gordos: 
Persona [peso=91.0]
Persona [peso=120.0]
    
answered by 05.07.2018 в 08:13