Respuesta :
import java.util.Scanner;
public class JavaApplication33 {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int total = 0;
System.out.println("Enter positive numbers (-1 to stop)");
while (true){
int num = scan.nextInt();
if (num == -1){
break;
}
else{
total += num;
}
}
System.out.println("Sum is "+total);
}
}
I hope this helps!
The program is an illustration of loops.
Loops are used to carry out repetition operations; examples are the for-loop and the while-loop.
The program in Java, where comments are used to explain each line is as follows:
import java.util.*;
public class Main
{
public static void main(String[] args) {
//This creates a scanner object
Scanner input = new Scanner(System.in);
//This initializes sum to 0, and declares num
int sum = 0; int num;
//This gets input from the user
num = input.nextInt();
//The loop is repeated until the user enters -1
while(num!=-1){
//This if-condition ensures that, only positive numbers are added
if(num > 0){
sum+=num;
}
//This gets another input from the user
num = input.nextInt();
}
//This prints the sum of all positive numbers gotten from the user
System.out.print(sum);
}
}
The above program is implemented using a while loop
Read more about similar programs at:
https://brainly.com/question/15263759