I have written a program for the question below, but my online course moderator has asked not to use
System.exit()
please help me with this, I have just started to learn program, I need this code without using System.exit()
I am giving my code below :
import java.util.Scanner;
public class AverageOfPositiveNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double s=0.0, k=0.0, neg=0.0, pos=0.0; double n=-1.0;
while (n!=0) {
n = Integer.valueOf(scanner.nextLine());
k++;
if (k==1 && n==0) {
System.out.println("Cannot calculate the average");
System.exit(0);
} if (n<0) {
neg++;
} else {
pos++;
s = s+n;
}
} double avg = (s/--pos);
if (k>1 && neg==k) {
System.out.println("Cannot calculate the average");
} else {
System.out.println(avg);
}
}
}
I am also attaching the question: