Advertisement
lleu

Untitled

Sep 25th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. //import scanner
  2. import java.util.Scanner;
  3.  
  4. public class Counts {
  5.  
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8.  
  9. //Assign variables
  10.  
  11. int totalCount = 0, posNum = 0, negNum = 0, totalEntered = 0;
  12. int userInput;
  13. float averageEntered;
  14.  
  15. //Begin user input
  16.  
  17. Scanner input = new Scanner(System.in);
  18. System.out.println("Please enter as many whole numbers as you would like. Enter 0 to end the program and get your results");
  19.  
  20. //Do while loop
  21.  
  22. do { userInput = input.nextInt();
  23. totalEntered += userInput;
  24. totalCount++;
  25.  
  26. if(userInput > 0) {
  27. posNum++;}
  28.  
  29. else if(userInput < 0){
  30. negNum++;}
  31.  
  32. }
  33. while(userInput != 0);
  34.  
  35. //Remove the 0 from count and calculate the average
  36. totalCount--;
  37. averageEntered = totalEntered / totalCount;
  38.  
  39. //Output
  40. System.out.println("You entered " + totalCount + " numbers");
  41. System.out.println("You entered " + posNum + " positive numbers");
  42. System.out.println("You entered " + negNum + " negative numbers");
  43. System.out.println("The total of the numbers entered was "+ totalEntered);
  44. System.out.printf("And the average of the numbers was %.2f", averageEntered);
  45.  
  46. //Close user input
  47. input.close();
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement