Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Myk {
  4.  
  5.     public static void main(String[] args) {
  6.         List<String> numbers = new LinkedList<String>();
  7.         Scanner scanner = new Scanner(System.in);
  8.        
  9.         System.out.println("Provide a number or type \"exit\" to end.");
  10.         String input = scanner.next();
  11.         while (! input.equals("exit")) {
  12.             System.out.println("Provide a number or type \"exit\" to end.");
  13.             numbers.add(input);
  14.             input = scanner.next();
  15.         }
  16.         double result = dodaj(numbers.toArray(new String[1]));
  17.         System.out.println("Result: " + result);
  18.     }
  19.  
  20.     public static double dodaj(String... args) {
  21.         double suma = 0;
  22.         try {
  23.             for (int i = 0; i < args.length; i++)
  24.                 suma += Double.valueOf(args[i]);
  25.         } catch (NumberFormatException e) {
  26.             System.out.println("At least one of provided values is not a number!");
  27.             System.exit(1);
  28.         }
  29.         return suma;
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement