Write a Java program that inputs five real numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input
Write a Java program that inputs five real numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int negativeCount = 0;
int positiveCount = 0;
int zeroCount = 0;
System.out.println("Enter five real numbers:");
for (int i = 0; i < 5; i++) {
if (scanner.hasNextDouble()) {
double number = scanner.nextDouble();
if (number < 0) {
negativeCount++;
} else if (number > 0) {
positiveCount++;
} else {
zeroCount++;
}
} else {
System.out.println("Invalid input. Expected five real numbers.");
break;
}
}
System.out.println("Number of negative numbers: " + negativeCount);
System.out.println("Number of positive numbers: " + positiveCount);
System.out.println("Number of zeros: " + zeroCount);
}
}
need an explanation for this answer? contact us directly to get an explanation for this answer