Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import java.util.*;
  2. public class hasnextex
  3. {
  4. static Scanner kb = new Scanner(System.in);
  5. public static void main(String[] args)
  6. {
  7.  
  8. // number entered and the running total
  9. int inputNum, sum = 0;
  10.  
  11. // count of valid and invalid numbers seen
  12. int numValid = 0, numInvalid = 0;
  13. int avgl;
  14. double average;
  15.  
  16.  
  17. System.out.print("Enter a positive value (EOF to quit): ");
  18. while ( kb.hasNext() )
  19. {
  20. inputNum = kb.nextInt();
  21. if (inputNum >= 0)
  22. {
  23. sum = sum + inputNum;
  24. numValid = numValid + 1;
  25. }
  26. else
  27. {
  28. System.out.println("The number " + inputNum + " is invalid.");
  29. numInvalid = numInvalid + 1;
  30. }
  31. System.out.print("Enter a positive value (EOF to quit): ");
  32. }
  33.  
  34. avgl = 100 * sum / numValid;
  35. average = avgl / 100;
  36.  
  37. System.out.println("There were " + numValid + " valid numbers entered");
  38. System.out.printf("The sum of the valid numbers was " + sum + " and the average was " + average + ". ");
  39. System.out.println("There were " + numInvalid + " invalid numbers.");
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement