I need this exercise in neatbeans please help! I'm starting [closed]

-2

Design an algorithm to which you enter numbers, add the positive ones, but when entering 3 times a negative number you must exit the program and show the sum of the positive ones.

    
asked by Andres Cadena 10.03.2016 в 03:22
source

1 answer

5

First you must import the libraries:

import java.io.BufferedReader;
import java.io.InputStreamReader;

Then you create the instance

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

Then you create the necessary variables:

int total = 0;
int num;
int contador_negativos = 0;

Now we proceed to request the information:

while(contador_negativos < 3){
    System.out.println("Ingrese un número: ");
    num = Integer.parseInt(br.readLine());
    if(num < 0){
        contador_negativos++;
    }else{
        total += num;
    }
}
System.out.println("El resultado es: " + total );
    
answered by 10.03.2016 в 03:53