Answer:
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
compute_pos();
}
public static void compute_pos(){
Scanner scan = new Scanner(System.in);
int sum = 0;
int counter = 0;
// Prompt the user to enter an input
System.out.println("Enter your value");
// user input is assign to the variable value
int value = scan.nextInt();
while(value != 0){
if (value > 0){
sum += value;
counter++;
}
System.out.println("Enter your value");
value = scan.nextInt();
}
// Display the value of sum to the user
System.out.println("The sum of the positive number is: " + sum);
// Display the value of counter to the user
System.out.println("The number of positive number is: " + counter);
}
}
Explanation:
The first line import the Scanner class which allow the program to read input from the user. The class Solution is then defined. Then the main method which signify the beginning of execution in the program is defined. A method is called inside the main method.
The method compute_pos is defined. Inside the compute_pos method, a scanner object scan is declared to accept input from the user via the keyboard.
The variable sum and counter is declared and assigned to 0.
A prompt is displayed to the user to input a value. The user input is accepted and stored in the variable value.
Then a while loop is use to check the user input if it is not equal to zero. If it is equal to zero, the loop will end. Inside the loop, we first check to see if the user input is greater than 0, if it is, we add it to the variable sum and increase our counter by 1. At the end of the loop, a prompt is again display to accept input from the user.
Outside the loop, we display the sum and counter to the user.