Sum of results

-2

Comrades good night, I would like your help to be able to obtain the sum of the result of all the variances stored in all the numbers of activities that are carried out, this is my code.

float [] numbers = new float [10];         System.out.println ("Number of Activities");         int n = lector.nextInt ();         for (int i = 0; i

    System.out.println("Tiempo optimista: ");
    a=lector.nextFloat();
    System.out.println("Tiempo mas probable: ");
    b=lector.nextFloat();
    System.out.println("Tiempo pesimista: ");
    c=lector.nextFloat();
    prom = (a+4*b+c)/6;
    System.out.println("El tiempo probable es: " + prom);
    var = ((c-a)/6)*2;
    System.out.println("La varianza es de: " + var);
    
asked by German 08.11.2017 в 02:53
source

1 answer

0

A possible solution would be to add a variable before going through the for that is the sum of the variances and within the for you should add the value that has + the moment variance.

Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        float a = 0, b = 0, c = 0, var = 0, prom = 0;

        // [AGREGADO] Se declara suma
        float suma = 0;

        Scanner lector = new Scanner(System.in);
        System.out.println("Numero de Actividades ");

        int n = lector.nextInt();
        for (int i = 0; i < n; i++) {

            System.out.println("Tiempo optimista: ");
            a = lector.nextFloat();
            System.out.println("Tiempo mas probable: ");
            b = lector.nextFloat();
            System.out.println("Tiempo pesimista: ");
            c = lector.nextFloat();
            prom = (a + 4 * b + c) / 6;
            System.out.println("El tiempo probable es: " + prom);
            var = ((c - a) / 6) * 2;
            System.out.println("La varianza es de: " + var);

            // [AGREGADO] Por cada varianza que se genere, se sumará su valor a suma.
            suma = suma + var;
        }

        // [AGREGADO] Se imprime la suma de varianzas
        System.out.println("La suma de las varianzas es: " + suma);

        lector.close();
    }

}

Clarification:
When asking, try to write all the code, not just a fragment. The code is cut and I'm not sure if it's what you need, so I can only base your order without looking at the code

    
answered by 08.11.2017 в 03:13