Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exercise9;
- import java.util.Scanner;
- /**
- * @author Marvin Exercise9 - Sum and Average
- *
- * Read, from standard input a series of integers, one per line, ending
- * in a sentinel value of 999. The numbers will be in the range 0 to
- * 100. Output on a line Sum: followed by the sum of the series of
- * integers as shown below. Output on a new line Average: followed by
- * the mean average of the series of numbers formatted to a precision of
- * 2 decimal places as shown below
- */
- public class Exercise9 {
- private final static int SENTINEL_VALUE = 999;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- boolean finished = false;
- int counter = 0;
- int values = 0;
- System.out.println("Please insert random numbers");
- while (!finished) {
- if (scanner.hasNextInt()) {
- int enteredValue = scanner.nextInt();
- if (enteredValue == Exercise9.SENTINEL_VALUE) {
- finished = true;
- } else {
- values += enteredValue;
- counter++;
- }
- } else {
- scanner.next();
- System.out.println("In this program are only numbers allowed");
- }
- }
- if (counter != 0)
- System.out.printf("Sum: %d \nAverage: %.2f", values, (values / (double) counter));
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement