Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tells how many numbers are positive and negative. Plus prints total.
- //Prepared by Ethan Glover
- import java.util.Scanner;
- public class Prog4i1 {
- public static void main(String[] args) {
- //Ask for user input
- Scanner input = new Scanner(System.in);
- System.out.println("Enter an integer, the input ends if it is 0: ");
- //Set variables
- int total = 0;
- double average = 0.0;
- int numOfPos = 0;
- int numOfNeg = 0;
- int numOfNum = 0;
- //Calcuate numbers
- int value = input.nextInt();
- while (value != 0) {
- //Count variables
- numOfNum++;
- total += value;
- if (value > 0) {
- numOfPos++;
- }
- else {
- numOfNeg++;
- }
- }
- //Caculate average
- average = total/numOfNum;
- //Display output
- if (total > 0) {
- System.out.println("The number of positives is " + numOfPos);
- System.out.println("The number of negatives is " + numOfNeg);
- System.out.println("The total is " + total);
- System.out.println("The average is " + average);
- }
- else {
- System.out.println("No numbers are entered except 0");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement