Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Myk {
- public static void main(String[] args) {
- List<String> numbers = new LinkedList<String>();
- Scanner scanner = new Scanner(System.in);
- System.out.println("Provide a number or type \"exit\" to end.");
- String input = scanner.next();
- while (! input.equals("exit")) {
- System.out.println("Provide a number or type \"exit\" to end.");
- numbers.add(input);
- input = scanner.next();
- }
- double result = dodaj(numbers.toArray(new String[1]));
- System.out.println("Result: " + result);
- }
- public static double dodaj(String... args) {
- double suma = 0;
- try {
- for (int i = 0; i < args.length; i++)
- suma += Double.valueOf(args[i]);
- } catch (NumberFormatException e) {
- System.out.println("At least one of provided values is not a number!");
- System.exit(1);
- }
- return suma;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement