Write a Java program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner; public class JavaLoopExcercise { public static void main(String[] args) { Scanner console = new Scanner(System.in); int number, countPositive = 0, countNegative = 0, countZero = 0; char choice; do { System.out.print("Enter the number "); number = console.nextInt(); if(number > 0) { countPositive++; } else if(number < 0) { countNegative++; } else { countZero++; } System.out.print("Do you want to continue y/n? "); choice = console.next().charAt(0); }while(choice=='y' || choice == 'Y'); System.out.println("Positive numbers: " + countPositive); System.out.println("Negative numbers: " + countNegative); System.out.println("Zero numbers: " + countZero); } }
Result:
Enter the number 5
Do you want to continue y/n? y
Enter the number -5
Enter the number 0
Do you want to continue y/n? n
Positive numbers: 2
Negative numbers: 1
Zero numbers: 1
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Enter the number 5
Do you want to continue y/n? y
Enter the number -5
Do you want to continue y/n? y
Enter the number 5
Do you want to continue y/n? y
Enter the number 0
Do you want to continue y/n? n
Positive numbers: 2
Negative numbers: 1
Zero numbers: 1
need an explanation for this answer? contact us directly to get an explanation for this answer