Why does not the while function?

1

I have to make a program and it does not work for me, the main problem is with the average, if I put it in while it prints me that it gives all zero, and I put it outside the while I jump error

  

("Exception in thread" main "java.lang.ArithmeticException: / by zero"   ) and something about division by zero.

Now I leave the statement:

  

Given the salaries and categories of employees it is requested to obtain.   A) Amount of salaries < 10000 in category 1. B) Amount of salaries

     
    

= 12000 in category 2 and salaries 15000. D) Average of the salaries of each     category . The exercise ends when a category is entered     equal to 0.

  
int sueldo;
        int categoria = 0;
        int cantidadCate1 = 0;
        int cantidadCate2 = 0;
        int cantidadCate3 = 0;
        int cantidadSueldo = 0;
        int sumatoria1 = 0;
        int sumatoria2 = 0;
        int sumatoria3 = 0;
        int promedio1 = 0;
        int promedio2 = 0;
        int promedio3 = 0;

        Scanner teclado = new Scanner(System.in);

        while (categoria != 0) {

            System.out.println("Ingrese categoria : ");
            categoria = teclado.nextInt();

            System.out.println("Ingrese sueldo : ");
            sueldo = teclado.nextInt();

            if (categoria == 1) {

                if (sueldo < 10000) {
                    cantidadCate1++;

                }

                sumatoria1 += sueldo;
            }

            if (categoria == 2) {

                if (sueldo >= 12000) {

                    cantidadCate2++;
                }

                sumatoria2 += sueldo;
            }

            if (categoria == 3) {

                if (sueldo <= 15000) {

                    cantidadCate3++;

                }

                sumatoria3 += sueldo;
            }

            if (sueldo > 15000) {

                cantidadSueldo++;
            }

            promedio1 = sumatoria1 / cantidadCate1;
            promedio2 = sumatoria2 / cantidadCate2;
            promedio3 = sumatoria3 / cantidadCate3;

        }

        System.out.println("a)Cantidad de sueldos <10000 en la categoria 1 : " + cantidadCate1);
        System.out.println("b1)Cantidad de sueldos >= 12000 en la categoira 2 : " + cantidadCate2);
        System.out.println("b2)Cantidad de de sueldos <= 15000 en la categoira 3 : " + cantidadCate3);
        System.out.println("c)Cantidad de sueldos > 15000 : " + cantidadSueldo);
        System.out.println("d)Promedio de los sueldos de cada categoria : ");
        System.out.println("Promedio de la categoria 1 : " + promedio1);
        System.out.println("Promedio de la categoria 2 : " + promedio2);
        System.out.println("Promedio de la categoria 3 : " + promedio3);
    
asked by computer96 21.08.2018 в 01:59
source

2 answers

1

To enter the while categoria must be different from 0 at first, you can put any value, in this case I initialized it with 1. The averages are going to be calculated out of the while, since you have the total sum of the salaries, to avoid that when you put 0 you ask for the salary, I added a small if you skip the steps, and at the end, to take the average your error is due to the division between 0, for it is validated that if the amount and the sum must be greater than 0.

public static void main(String[] args) {
        int sueldo;
        int categoria = 1; //Aquí inicializas en un valor diferente a 0
        int cantidadCate1 = 0;
        int cantidadCate2 = 0;
        int cantidadCate3 = 0;
        int cantidadSueldo = 0;
        int sumatoria1 = 0;
        int sumatoria2 = 0;
        int sumatoria3 = 0;
        int promedio1 = 0;
        int promedio2 = 0;
        int promedio3 = 0;

        Scanner teclado = new Scanner(System.in);

        while (categoria != 0) {
            System.out.println("Ingrese categoria : ");
            categoria = teclado.nextInt();

            if (categoria != 0) {//Este if sirve para que no te pida el sueldo
                System.out.println("Ingrese sueldo : ");
                sueldo = teclado.nextInt();
                if (categoria == 1) {
                    if (sueldo < 10000) {
                        cantidadCate1++;
                    }
                    sumatoria1 += sueldo;
                }
                if (categoria == 2) {
                    if (sueldo >= 12000) {
                        cantidadCate2++;
                    }
                    sumatoria2 += sueldo;
                }
                if (categoria == 3) {
                    if (sueldo <= 15000) {
                        cantidadCate3++;
                    }
                    sumatoria3 += sueldo;
                }
                if (sueldo > 15000) {
                    cantidadSueldo++;
                }
            }
        }

        //Validaciones que no sean divisiones entre 0

        if (sumatoria1 > 0 && cantidadCate1 > 0) {
            promedio1 = sumatoria1 / cantidadCate1;
        }
        if (sumatoria2 > 0 && cantidadCate2 > 0) {
            promedio2 = sumatoria2 / cantidadCate2;
        }
        if (sumatoria3 > 0 && cantidadCate3 > 0) {
            promedio3 = sumatoria3 / cantidadCate3;
        }

        System.out.println("a)Cantidad de sueldos <10000 en la categoria 1 : " + cantidadCate1);
        System.out.println("b1)Cantidad de sueldos >= 12000 en la categoira 2 : " + cantidadCate2);
        System.out.println("b2)Cantidad de de sueldos <= 15000 en la categoira 3 : " + cantidadCate3);
        System.out.println("c)Cantidad de sueldos > 15000 : " + cantidadSueldo);
        System.out.println("d)Promedio de los sueldos de cada categoria : ");
        System.out.println("Promedio de la categoria 1 : " + promedio1);
        System.out.println("Promedio de la categoria 2 : " + promedio2);
        System.out.println("Promedio de la categoria 3 : " + promedio3);
    }

Greetings

    
answered by 21.08.2018 / 08:06
source
2

Previously I had told you that in the case in which no category is selected the values of cantidadCate1, cantidadCate2 y cantidadCate3 , would have the value of zero, which would cause the error:

  

("Exception in thread" main "java.lang.ArithmeticException: / by   zero ")

For this you must validate only perform the operation when any of the variables does not have zero value:

    if(cantidadCate1 > 0)
    promedio1 = sumatoria1 / cantidadCate1;
    if(cantidadCate2 > 0)
    promedio2 = sumatoria2 / cantidadCate2;
    if(cantidadCate3 > 0)
    promedio3 = sumatoria3 / cantidadCate3;
    
answered by 21.08.2018 в 02:06