Answer:
Here's an example Java program that prompts the user to enter a series of positive numbers and calculates their sum. The program uses a while loop to repeatedly ask the user for input until a negative number is entered, at which point the loop terminates and the sum is displayed.
import java.util.Scanner;
public class PositiveNumberSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
int number;
System.out.println("Enter positive numbers (enter a negative number to exit):");
while (true) {
number = input.nextInt();
if (number < 0) {
break;
}
sum += number;
}
System.out.println("The sum of the positive numbers is " + sum);
}
}
In this program, we first create a new Scanner object to read input from the console. We also initialize the sum to 0 and the number to an arbitrary value.
We then prompt the user to enter positive numbers by displaying a message to the console. The while loop that follows reads input from the user using the nextInt() method of the Scanner object. If the number entered is negative, we break out of the loop using the break statement. Otherwise, we add the number to the sum using the += operator.
Finally, we display the sum of the positive numbers to the console using the println() method.