I can not find how to find the biggest

1

I'm giving it and I can not find how to find the number of students with grades above average.

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

    int n, cont = 0;
    double acum = 0, p_est, p_gen = 0;

    //IN
    System.out.println("Ingrese numero de estudiantes ");
    n = sc.nextInt();

    //PROCESS
    for (int i = 1; i <= n; i++) {

        System.out.println("Ingrese promedio del estudiante " + i + " : ");
        p_est = sc.nextDouble();

        acum = acum + p_est;
        p_gen = acum / n;

        if (p_est > p_gen) {
            cont++;

        }

    }

    System.out.println("El promedio general es: " + p_gen);
    System.out.print("Estudiantes superiores: " + cont);
    System.out.println("");

  }
}
    
asked by iSony 04.09.2017 в 20:26
source

3 answers

4

First you need to get the average and then compare with each of the students. You can use an array. Try this way:

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

    int n, cont = 0;
    double acum = 0, p_gen = 0;

    //IN
    System.out.println("Ingrese numero de estudiantes ");
    n = sc.nextInt();
    double [] promedios = new double[n];

    //PROCESS
    for (int i = 0; i < n; i++) {
        System.out.println("Ingrese promedio del estudiante " + (i + 1) + " : ");
        promedios[i] = sc.nextDouble();
        acum = acum + promedios[i];
    }
    p_gen = acum / n;
    for (int i = 0; i < n; i++) {
        if(promedios[i] > p_gen)
           cont ++;
    }

    System.out.println("El promedio general es: " + p_gen);
    System.out.print("Estudiantes superiores: " + cont);
    System.out.println("");

}
    
answered by 04.09.2017 / 20:37
source
0

Create a variable called max which will take the value of the highest average that is fenced by entering, then show it by console.

Here I leave the code with the function to know which is the biggest:

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

    int n, cont = 0;
    double acum = 0, p_est, p_gen = 0;
    double max = 0;    
    //IN
    System.out.println("Ingrese numero de estudiantes ");
    n = sc.nextInt();

    //PROCESS
    for (int i = 1; i <= n; i++) {

        System.out.println("Ingrese promedio del estudiante " + i + " : ");
        p_est = sc.nextDouble();

        acum = acum + p_est;
        p_gen = acum / n;

        if (p_est > p_gen) {
            cont++;

        }
        if ( max<p_est){
            max = p_est;
        }

    }

    System.out.println("El promedio general es: " + p_gen);
    System.out.println("Estudiantes superiores: " + cont);
    System.out.println("Estudiante con promedio mas alto: " + max);
    System.out.println("");

  }
}

    
answered by 04.09.2017 в 20:42
0

Create a variable that stores the average and then compare it with all students.

Example

public class PromedioEstudiantes
{
    private Scanner input = new Scanner(System.in);

    public void procesarCaso()
    {
        System.out.println("Ingrese el numero de estudiantes: ");
        int numeroEstudiantes = input.nextInt();

        double[] estudiantes = rellenarArreglo(numeroEstudiantes);
        double promedio = calcularPromedioEstudiantes(estudiantes);
        int estudiantesSuperiores = obtenerEstudiantesSuperiores(estudiantes, promedio);

        System.out.println("El promedio es: " + promedio);
        System.out.println("Hay " + estudiantesSuperiores + " superiores al promedio");
    }

    private double[] rellenarArreglo(int numeroEstudiantes)
    {
        double[] estudiantes = new double[numeroEstudiantes];
        for (int i = 0; i < numeroEstudiantes; i++)
        {
            System.out.println("Ingrese promedio del estudiante " + (i + 1) + ": ");
            estudiantes[i] = input.nextInt();
        }
        return estudiantes;
    }

    private int obtenerEstudiantesSuperiores(double[] estudiantes, double promedio)
    {
        int estudiantesSuperiores = 0;
        for (double estudiante : estudiantes)
        {
            if (estudiante > promedio) estudiantesSuperiores++;
        }
        return estudiantesSuperiores;
    }

    private double calcularPromedioEstudiantes(double[] estudiantes)
    {
        double sumaNotas = 0.0d;
        for (double estudiante : estudiantes)
        {
            sumaNotas += estudiante;
        }
        return sumaNotas / estudiantes.length;
    }

    public static void main(String[] args)
    {
        new PromedioEstudiantes().procesarCaso();
    }
}

If you need to know which students have what average, use a HashMap .

    
answered by 04.09.2017 в 20:56