Skip to content

Instantly share code, notes, and snippets.

@opensourcekam
Created October 16, 2014 19:31
Show Gist options
  • Save opensourcekam/427031cebdb57d86b7e1 to your computer and use it in GitHub Desktop.
Save opensourcekam/427031cebdb57d86b7e1 to your computer and use it in GitHub Desktop.
Count positive and negative numbers and compute the average of numbers
//Kameron Robinson LAB 5.1 10/16/2014
import java.util.*;
public class countAverageKAR{
public static void main(String[] args)
{
int pos = 0;
int neg = 0;
int sum = 0;
int total = 0;
double average = 0.0;
//Scanner
Scanner input = new Scanner(System.in);
System.out.println("Enter an interger, the input ends if it's 0: ");
int enter = input.nextInt();
while( enter != 0 ){
/*exit if there is a 0
if (enter == 0){
System.out.println("The only number entered is " + enter);
}*/
//if statement that counts positive numbers
if (enter > 0){
pos = pos + 1;
}
//constant if statement that counts negative numbers
else{
neg = neg + 1;
}
//Calculates the sum
sum = enter + sum;
System.out.println("Enter another number: ");
enter = input.nextInt();
//Calculate average
average = (double) sum /(pos + neg);
}
if( average != 0 ){
System.out.println("You have " + pos + " positive numbers");
System.out.println("You have " + neg + " negative numbers");
System.out.println("Your total is: " + (double)sum);
System.out.println("You have an average of " + average);
}
else
System.out.println("The only number entered is " + enter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment