Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Enter positive integers, one number per line, ending with -1
- 56 46 47 31 11
- The sum of the numbers is 191
- The average is 38.2
- package javaapplication4;
- import java.io.*;
- public class JavaApplication4 {
- public static void main(String[] args) throws IOException{
- InputStreamReader in = new InputStreamReader(System.in);
- BufferedReader keyboard = new BufferedReader(in);
- int num;
- int sum;
- int count;
- double average;
- System.out.println("Enter positive integers, one number per" +
- "line, ending with ..1");
- count = 0;
- sum = 0;
- num = Integer.parseInt(keyboard.readLine());
- while(num != -1){ sum = sum + num;
- count++;
- num = Integer.parseInt(keyboard.readLine());
- }
- System.out.println("The sum of the number is: " + sum);
- if(count !=0)
- average = sum / count;
- else
- average = 0;
- System.out.println("The average is: "+ average);
- }
- }
- Enter positive integers, one number perline, ending with ..1
- 23 32
- Exception in thread "main" java.lang.NumberFormatException: For input string: " 23 32"
- Enter positive integers, one number perline, ending with ..1
- 5
- 6
- -1
- The sum of the number is: 11
- The average is: 5.0
- int sum = 0;
- int count = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement