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.
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.
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 );