How to solve average within the for? Java Cycle For

0
  • It is requested to develop the algorithm to manage the grades of the students of basic programming I.
  • It is known that the number of students is 10.

    You should be able to enter:

    a. Student ID b. Career (w - Technician in Development of Web Applications; - Technician in Development of Mobile apps) c. First Partial Note d. Second Partial Note and. Note Practical Work

    It must be calculated:

    a. Average grade for each student b. Number of students of the Technician in Development of Web Applications c. Number of students of the Technician in Development of Mobile Applications d. Average Web notes and. Average mobile notes

    * I do not know how to do the average of Web and Mobile notes. Since the way I did it gives me any result.

    int alumnos = 10;
        int dni = 0;
        String carrera = " ";
        int parcial1W=0;
        int parcial1M=0;
        int parcial2W=0;
        int parcial2M=0;
        int notaTPW =0;
        int notaTPM =0;
        int promedioW = 0;
        int promedioM = 0;
        int cantidadAlumMobile = 0;
        int cantidadAlumWeb=0;
        int promedioWeb=0;
        int sumaW =0;
        int sumaM=0;
        int sumatoriaPW = 0;
        int sumatoriaPM=0;
        int promedioNWeb =0;
        int promedioNMobile=0;
    
        Scanner teclado = new Scanner (System.in);
    
        for (int i = 1; i < alumnos; i++) {
    
    
            System.out.println(i+".Ingrese DNI : ");
            dni = teclado.nextInt();
            System.out.println(i+".Ingrese Carrera : ");
            carrera = teclado.next();
    
            switch(carrera) {
    
    
            case "Web":
    
                System.out.println(i+".Ingrese nota del 1er parcial : ");
                parcial1W = teclado.nextInt();
    
                System.out.println(i+"Ingrese nota del 2do parcial : ");
                parcial2W = teclado.nextInt();
    
                System.out.println(i+"Ingrese nota del T.P : ");
                notaTPW = teclado.nextInt();
    
                sumaW = parcial1W + parcial2W + notaTPW ;
                promedioW = sumaW/3;
    
                System.out.println(i+".Promedio : "+promedioW);
    
                cantidadAlumWeb++;
                promedioW++;
    
                break;
    
            case "Mobile" :
    
                System.out.println(i+".Ingrese nota del 1er parcial : ");
                parcial1M = teclado.nextInt();
    
                System.out.println(i+"Ingrese nota del 2do parcial : ");
                parcial2M = teclado.nextInt();
    
                System.out.println(i+"Ingrese nota del T.P : ");
                notaTPM = teclado.nextInt();
    
                sumaM = parcial1M + parcial2M + notaTPM;
                promedioM = sumaM/3;
    
                System.out.println(i+".Promedio : "+promedioM);
    
                cantidadAlumMobile++;
                promedioM++;
    
                break;
    
                default :
    
                    System.out.println("Error! Carrera Inexistente.");
    
                    break;
    
    
    
            }
    
            sumatoriaPW+=promedioW;
            sumatoriaPM+=promedioM;
    
        }
    
    
    
        promedioNWeb = sumatoriaPW / cantidadAlumWeb;
        promedioNMobile = sumatoriaPM / cantidadAlumMobile;
    
        System.out.println("Promedio Notas WEB : "+promedioNWeb);
        System.out.println("Promedio Notas Mobile : "+promedioNMobile);
    
        System.out.println("Cantidad de alumnos en Tec.Web : "+cantidadAlumWeb);
    
        System.out.println("Cantidad de alumnos en Tec.Mobile : "+cantidadAlumMobile);
    
        
    asked by computer96 05.10.2018 в 01:26
    source

    1 answer

    1

    the little mistake is in

    sumatoriaPW+=promedioW;
    sumatoriaPM+=promedioM;
    

    since when you enter for example a "Web", and the next one "Mobile" the value assigned to "averageW" was never equal to 0, so it adds the previous value, it would be advisable to save each one in its respective "case:" or after completing the sum, equal to 0 both for example:

    sumatoriaPW+=promedioW;
    promedioW = 0;
    sumatoriaPM+=promedioM;
    promedioM = 0;
    

    You also have a

    promedioM++;
    promedioW++;
    

    to be eliminated, and finally, a small recommendation, all the averages that are calculated or stored should always be in variables of real type (float, double) and the inside of "for", you should start the condition in 0 or add 1 to the number of students, I leave the code:

    public class main {
    
        public static void main(String[] args) {
            int alumnos = 4;
            int dni = 0;
            String carrera = " ";
            int parcial1W = 0;
            int parcial1M = 0;
            int parcial2W = 0;
            int parcial2M = 0;
            int notaTPW = 0;
            int notaTPM = 0;
            float promedioW = 0;
            float promedioM = 0;
            int cantidadAlumMobile = 0;
            int cantidadAlumWeb = 0;
            int promedioWeb = 0;
            int sumaW = 0;
            int sumaM = 0;
            float sumatoriaPW = 0;
            float sumatoriaPM = 0;
            float promedioNWeb = 0;
            float promedioNMobile = 0;
    
            Scanner teclado = new Scanner(System.in);
    
            for (int i = 1; i < alumnos; i++) {
                System.out.println(i + ".Ingrese DNI : ");
                dni = teclado.nextInt();
                System.out.println(i + ".Ingrese Carrera : ");
                carrera = teclado.next();
                switch (carrera) {
                    case "Web":
                        System.out.println(i + ".Ingrese nota del 1er parcial : ");
                        parcial1W = teclado.nextInt();
                        System.out.println(i + "Ingrese nota del 2do parcial : ");
                        parcial2W = teclado.nextInt();
                        System.out.println(i + "Ingrese nota del T.P : ");
                        notaTPW = teclado.nextInt();
                        sumaW = parcial1W + parcial2W + notaTPW;
                        promedioW = sumaW / 3;
                        System.out.println(i + ".Promedio : " + promedioW);
                        cantidadAlumWeb++;
                        sumatoriaPW += promedioW;
                        break;
                    case "Mobile":
                        System.out.println(i + ".Ingrese nota del 1er parcial : ");
                        parcial1M = teclado.nextInt();
                        System.out.println(i + "Ingrese nota del 2do parcial : ");
                        parcial2M = teclado.nextInt();
                        System.out.println(i + "Ingrese nota del T.P : ");
                        notaTPM = teclado.nextInt();
                        sumaM = parcial1M + parcial2M + notaTPM;
                        promedioM = sumaM / 3;
                        System.out.println(i + ".Promedio : " + promedioM);
                        cantidadAlumMobile++;
                        sumatoriaPM += promedioM;
                        break;
                    default:
                        System.out.println("Error! Carrera Inexistente.");
                        break;
                }
            }
    
            promedioNWeb = sumatoriaPW / cantidadAlumWeb;
            promedioNMobile = sumatoriaPM / cantidadAlumMobile;
    
            System.out.println("Promedio Notas WEB : " + promedioNWeb);
            System.out.println("Promedio Notas Mobile : " + promedioNMobile);
    
            System.out.println("Cantidad de alumnos en Tec.Web : " + cantidadAlumWeb);
            System.out.println("Cantidad de alumnos en Tec.Mobile : " + cantidadAlumMobile);
        }
    }
    

    Greetings

        
    answered by 05.10.2018 / 05:16
    source