multiplication and accumulation of several numbers entered by keyboard

2

I need if you can help me with the following I need to multiply all those numbers that are less than 0, entering it by keyboard, and go accumulating the multiplications.

Thanks

package control6;
import java.util.Scanner;
public class Control6 {

      public static void main(String[] args) {
    int sumaPositivos = 0;
    int multi = 0;
    int contarcero = 0;
    int acumulacionmulti = 0;
        Scanner sc=new Scanner(System.in);
        int n;
        do {
            System.out.println("Ingresa un numero, cuando quieras terminar escribe 99999: ");
            n= sc.nextInt();
            if(n==0){
                System.out.println("El numero "+n+" es Cero");
                contarcero++;
            } else{
                if(n>0 && n!= 99999){
                    System.out.println("El numero "+n+" es positivo");
                    sumaPositivos = sumaPositivos + n;
                }else{
                    if(n<0 && n!= 99999){
                    System.out.println("El numero "+n+" es negativo");
                    multi = n*=n;
                   acumulacionmulti = multi++;

                    }    
                }
            } 
        }while(n != 99999);

        System.out.println("la suma de los numeros positivos introducidos es:  "+sumaPositivos);
        System.out.println("la acumulacion multiplicada de los numeros negativos introducidos es:  "+multi);
        System.out.println("la cantidad de ceros ingresados es:  "+contarcero);
    }
}

that's the code I'm doing

    
asked by Danilo Antonio Lopez Vega 06.10.2018 в 06:59
source

2 answers

1

Me in my case, to go accumulating or saving what we are giving the multiplication of negative numbers, I would do it this way .. multi *= n; which is equivalent to this other multi = multi * n; .

But doing this we would have a problem , and that is that the first value of multi equals 0 , and if we go multiplying any number by 0 , would give us as result 0 . Therefore what I would do is put a condition that if the condition that the entered number is negative and the value of multi is 0, instead of multiplying it, it would be equal to the value of the number entered, and already to root of this the following times that we introduce negative values and yes, go multiplying it by the saved value.

if (n < 0 && n != 99999) {
     System.out.println("El numero " + n + " es negativo");
     if (multi == 0) {
        multi = n;
     } else {
        multi *= n;
     }
}

Another way, as @Zeugirdor has commented, is to initialize the multi variable with a value of 1 , and when you enter a negative value directly do the multiplication and accumulation.

int multi = 1;
...
if (n < 0 && n != 99999) {
    System.out.println("El numero " + n + " es negativo");
    multi *= n;
}
    
answered by 06.10.2018 / 09:02
source
0

After your help, I left the code in this way. Thank you very much.

package control6;
import java.util.Scanner;

public class Control6 {
    public static void main(String[] args) {
        int sumaPositivos = 0;
        int contarceros = 0;
        int acumulacionmulti = 1;
        Scanner Teclado = new Scanner(System.in);
        int n;
        do {
            System.out.println("Ingresa un numero, cuando quieras terminar escribe 99999: ");
            n = Teclado.nextInt();
            if (n == 0) {
                System.out.println("El numero ingresado " + n + " es Cero");
                contarceros++;
            } else {
                if (n > 0 && n != 99999) {
                    System.out.println("El numero ingresado " + n + " es positivo");
                    sumaPositivos = sumaPositivos + n;
                } else {
                    if (n < 0 && n != 99999) {
                        System.out.println("El numero ingresado " + n + " es negativo");
                        acumulacionmulti *= n;

                    }
                }
            }
        } while (n != 99999);

        System.out.println("la suma de los numeros positivos introducidos es:  " + sumaPositivos);
        System.out.println("la acumulacion multiplicada de los numeros negativos introducidos es:  " + acumulacionmulti);
        System.out.println("la cantidad de ceros ingresados es:  " + contarceros);
    }
}
    
answered by 13.10.2018 в 05:35