Error removing student media and / or failed subjects in Java

1

I am doing an exercise for class in which you have to ask for the number of students and the subjects of each student. Once this information is known, the 3 notes per module are requested and the average is made only if you have all approved (the 3 notes of all the modules).

The problem is that it makes me average, and I get the error that there is some suspense, but only in the last 3 notes that I enter by keyboard. That is, if the student has two modules and one of the notes is suspended in the first module, and the second is approved, I get the average instead of the error.

//CLASE ALUMNOS
package NotasAlumnos;

import utilidadaesGenerales.Utilidades;

/**
 *
 * @author Anna
 */
public class Alumno {

    private String nombre, nomAsig;
    private float nota1, nota2, nota3;
    private int cantidadMod;
    private int contador = 1;
    private float media[], mediaTotal;
    private boolean aprobado;

    public void pedirNombre() {
        nombre = Utilidades.pedirCadena("\n" + Utilidades.Colores.CYAN + "\tIntroduzca su nombre: ");
    }

    public void pedirCantModulos() {
        cantidadMod = Utilidades.pedirNumeroEntero("\n" + Utilidades.Colores.CYAN + "\t" + nombre + " ,introduzca su número de asignaturas: ");
    }

    public void pedirNotas() {
        media = new float[cantidadMod];
        aprobado = true;
        for (int cont = 0; cont < this.cantidadMod; cont++) {

            System.out.println("\n" + Utilidades.Colores.CYAN + "\t\tVamos a introudocir las notas para la asignatura " + contador++);
            nota1 = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la primera evaluación : ");
            nota2 = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la segunda evaluación:  ");
            nota3 = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca la nota de la tercera evaluación: ");

            media[cont] = (nota1 + nota2 + nota3) / 3;
            //mediaTotal += media;

        }

        for (int i = 0; i < cantidadMod; i++) {

            if (nota1 < 5 || nota2 < 5 || nota3 < 5) {
                aprobado = false;
                System.out.println("Alguna de tus evaluaciones de la asignatura " + i + "  está suspensa y tienes que ir a septiembre");

            } else {
                System.out.println("lA MEDIA DEL MODULO " + (i + 1) + " ES " + media[i] + " " + nombre);

            }

        }

        // mediaTotal /= cantidadMod;
    }

    public void pedirTodo() {
        pedirNombre();
        pedirCantModulos();
        pedirNotas();
    }

    /* public float getMedia() {
        return media;
    }*/
    public float getMediaTotal() {
        return mediaTotal;
    }

    public int getCantidadMod() {
        return cantidadMod;
    }

    public String getNombre() {
        return nombre;
    }

    public String getNomAsig() {
        return nomAsig;
    }

    public float getNota1() {
        return nota1;
    }

    public float getNota2() {
        return nota2;
    }

    public float getNota3() {
        return nota3;
    }

    Alumno() {
        pedirTodo();
    }
}
//MAIN
package NotasAlumnos;

import java.util.Scanner;
import utilidadaesGenerales.Utilidades;

/**
 *
 * @author Anna
 */
public class PrincipalAlumnos {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int cantidadAl;
        float mediaNotas;

        Scanner pedir = new Scanner(System.in);
        System.out.print("\n"+Utilidades.Colores.CYAN+"\tIntroduzca el numero de alumnos: ");
        cantidadAl = pedir.nextInt();
        for (int i = 1; i <= cantidadAl;  i++ ) {
            Alumno alum = new Alumno();



        }

I think the error is the control variable aprobado , which is not doing anything to me, but I'm not sure how the Booleans work yet.

And after the answers received, I tried this, but now I get an error!

public void pedirNotas() {
    media = new float[cantidadMod];
    notas = new float [cantidadMod];
    aprobado = true;
    for (int cont = 0; cont < this.cantidadMod; cont++) {

        System.out.println("\n" + Utilidades.Colores.CYAN + "\t\tVamos a introudocir las notas para la asignatura " + contador++);
        notas[cont][0] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la primera evaluación : ");
        notas[cont][1] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la segunda evaluación:  ");
        notas[cont][2] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca la nota de la tercera evaluación: ");

        media[cont] = (notas[cont][0] + notas[cont][1] + notas[cont][2]) / 3;
        //mediaTotal += media;

    }

    for (int i = 0; i < cantidadMod; i++) {

        if (notas[i][0] < 5 || notas[i][1] < 5 || notas[i][2] < 5) {
            aprobado = false;
            System.out.println("Alguna de tus evaluaciones de la asignatura " + i + "  está suspensa y tienes que ir a septiembre");

        } else {
            System.out.println("lA MEDIA DEL MODULO " + (i + 1) + " ES " + media[i] + " " + nombre);

        }

I get an error in the lines of

 notas[cont][0] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la primera evaluación : ");
            notas[cont][1] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la segunda evaluación:  ");
            notas[cont][2] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca la nota de la tercera evaluación: ");

            media[cont] = (notas[cont][0] + notas[cont][1] + notas[cont][2]) / 3;
  

error: array requiered, but float found.

    
asked by AnnaPS 15.01.2017 в 12:29
source

2 answers

0

The problem is due to the fact that nota1, nota2, nota3 are shared among all the modules, and will keep reference to the notes of the last module entered, that is, this part of the code

...
        for (int i = 0; i < cantidadMod; i++) {

            if (nota1 < 5 || nota2 < 5 || nota3 < 5) {  
                aprobado = false;
                System.out.println("Alguna de tus evaluaciones de la asignatura " + i + "  está suspensa y tienes que ir a septiembre");

            } else {
                System.out.println("lA MEDIA DEL MODULO " + (i + 1) + " ES " + media[i] + " " + nombre);

            }

        }
...

will always return the same, and will be the average of the last module, if it is the case in which it approves, or an error otherwise.

The simplest solution is to save the notes of each module in a two-dimensional array

public void pedirNotas() {
    media = new float[cantidadMod];
    notas = new float[cantidadMod][3]; // debes crear el campo
    aprobado = true;
    for (int cont = 0; cont < this.cantidadMod; cont++) {

        System.out.println("\n" + Utilidades.Colores.CYAN + "\t\tVamos a introudocir las notas para la asignatura " + contador++);
        notas[cont][0] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la primera evaluación : ");
        notas[cont][1] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca su nota de la segunda evaluación:  ");
        notas[cont][2] = Utilidades.pedirNumeroFloat("\n" + Utilidades.Colores.MORADO + "\t\tIntroduzca la nota de la tercera evaluación: ");

        media[cont] = (notas[cont][0] + notas[cont][1] + notas[cont][2]) / 3;
        //mediaTotal += media;

    }

    for (int i = 0; i < cantidadMod; i++) {

        if (notas[i][0] < 5 || notas[i][1] < 5 || notas[i][2] < 5) {
            aprobado = false;
            System.out.println("Alguna de tus evaluaciones de la asignatura " + i + "  está suspensa y tienes que ir a septiembre");

        } else {
            System.out.println("lA MEDIA DEL MODULO " + (i + 1) + " ES " + media[i] + " " + nombre);

        }

    }

    // mediaTotal /= cantidadMod;
}

PS: If you use the previous solution, note1, note2, note3, they will not have any use, so you can delete them from the Student class, instead, you should create a field called notes, such as float [] []

    
answered by 15.01.2017 / 14:08
source
2

In your last answer you are stating notes as one-dimensional instead of two-dimensional.

Change

notas = new float [cantidadMod];

By

notas = new float [cantidadMod][3];

Look at the example of eduen-sarceño

    
answered by 16.01.2017 в 08:56