Advertisement
thorax232

Pos/Neg/Avg

Oct 13th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. //Tells how many numbers are positive and negative. Plus prints total.
  2. //Prepared by Ethan Glover
  3.  
  4. import java.util.Scanner;
  5. public class Prog4i1 {
  6. public static void main(String[] args) {
  7.  
  8.     //Ask for user input
  9.     Scanner input = new Scanner(System.in);
  10.     System.out.println("Enter an integer, the input ends if it is 0: ");
  11.    
  12.     //Set variables
  13.     int total = 0;
  14.     double average = 0.0;
  15.     int numOfPos = 0;
  16.     int numOfNeg = 0;
  17.     int numOfNum = 0;
  18.    
  19.     //Calcuate numbers
  20.     int value = input.nextInt();
  21.     while (value != 0) {
  22.    
  23.         //Count variables
  24.         numOfNum++;
  25.         total += value;
  26.         if (value > 0) {
  27.             numOfPos++;
  28.         }
  29.         else {
  30.             numOfNeg++;
  31.         }
  32.     }
  33.    
  34.     //Caculate average
  35.     average = total/numOfNum;
  36.    
  37.     //Display output
  38.     if (total > 0) {
  39.     System.out.println("The number of positives is " + numOfPos);
  40.     System.out.println("The number of negatives is " + numOfNeg);
  41.     System.out.println("The total is " + total);
  42.     System.out.println("The average is " + average);
  43.     }
  44.     else {
  45.     System.out.println("No numbers are entered except 0");
  46.     }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement