Error dividing by zero

-3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class EjercicioCalcularPromedio30alumnos { 
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        int edad = 0;
        int sumaedades = 0;
        float promedio = 0;

        for (int cantidad = 0; cantidad < 30; cantidad++) {
            do {
                System.out.println("Ingrese las notas de sus alumnos" + (cantidad + 1));
                edad = Integer.parseInt (bf.readLine());
            } while (!(edad > O));

            sumaedades = sumaedades + edad;
            promedio = sumaedades / cantidad;
        }

        System.out.println("su promedio es:" + promedio);
    }
}

The code does not work, when it worked 5 minutes ago.

Ingrese la nota de sus alumnos1
3
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at EjercicioCalcularPromedio30alumnos.main(EjercicioCalcularPromedio30alumnos.java:29)
C:\Users\se_ba\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
    
asked by Sebastian Ortiz 27.08.2017 в 00:57
source

1 answer

1

I leave an example that meets what you are looking for in an easier way

    public class Prueba {

    static Scanner entrada = new Scanner (System.in);

    public static void main(String[] args) {

        int edad = 0;
        int acumulador = 0;

        System.out.println("Cuantas personas?");
        int cantidadPersonas = entrada.nextInt();

        for (int i = 0; i < cantidadPersonas; i++)
        {
            System.out.println("Ingrese la edad de la persona N° " + (i+1));
            edad = entrada.nextInt();
            acumulador = acumulador + edad;
        }

        System.out.println("El promedio de la edad es : " + acumulador / cantidadPersonas);
    }

}

Your error is that you try to divide by zero in the first iteration of the loop. I hope to be helpful.

    
answered by 27.08.2017 / 01:34
source