Write a JAVA program that keeps asking the user for numbers, until the user enters 0. After the user enters 0, the program should display the number of negative and positive numbers entered. (I already wrote the code but it is not working, the output is just blank. I need help seeing what is going wrong in it.
- Write a JAVA program that keeps asking the user for numbers, until the user enters 0. After the user enters 0, the program should display the number of negative and positive numbers entered. (I already wrote the code but it is not working, the output is just blank. I need help seeing what is going wrong in it.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//creates a new scanner object
int n=1;
int negative_count=0;
//stores and counts the negative numbers
int positive_count=0;
//stores and counts the positive numbers
while (n!=0) {
// the stopping condition is zero.
n=sc.nextInt();
if (n<0){
negative_count=negative_count+1;
}
else if (n>0){
positive_count=positive_count+1;
}
}
System.out.println("Program Stopped Because User Entered Zero :( ");
System.out.println("Total Amount of Inputted Positive Numbers: "+positive_count);
System.out.println("Total Amount of Inputted Negative Numbers: "+negative_count);
}
}
Step by step
Solved in 4 steps with 3 images