Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. Enter positive integers, one number per line, ending with -1
  2. 56 46 47 31 11
  3. The sum of the numbers is 191
  4. The average is 38.2
  5.  
  6. package javaapplication4;
  7.  
  8. import java.io.*;
  9.  
  10. public class JavaApplication4 {
  11. public static void main(String[] args) throws IOException{
  12. InputStreamReader in = new InputStreamReader(System.in);
  13. BufferedReader keyboard = new BufferedReader(in);
  14.  
  15. int num;
  16. int sum;
  17. int count;
  18. double average;
  19.  
  20. System.out.println("Enter positive integers, one number per" +
  21. "line, ending with ..1");
  22. count = 0;
  23. sum = 0;
  24. num = Integer.parseInt(keyboard.readLine());
  25.  
  26. while(num != -1){ sum = sum + num;
  27. count++;
  28. num = Integer.parseInt(keyboard.readLine());
  29. }
  30. System.out.println("The sum of the number is: " + sum);
  31. if(count !=0)
  32. average = sum / count;
  33. else
  34. average = 0;
  35. System.out.println("The average is: "+ average);
  36. }
  37. }
  38.  
  39. Enter positive integers, one number perline, ending with ..1
  40. 23 32
  41. Exception in thread "main" java.lang.NumberFormatException: For input string: " 23 32"
  42.  
  43. Enter positive integers, one number perline, ending with ..1
  44. 5
  45. 6
  46. -1
  47. The sum of the number is: 11
  48. The average is: 5.0
  49.  
  50. int sum = 0;
  51. int count = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement