Advertisement
desislava_topuzakova

03. Sum Numbers

May 16th, 2020
1,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int sum = 0;
  7.  
  8.         //повтаряме: взимаме стойността на числото и да я добавяме сумата
  9.         //стоп: команда == "Stop"
  10.         //продълажваме: команда != "Stop"
  11.  
  12.         String command = scanner.nextLine(); //stop или число под формата на стринг
  13.         while (!command.equals("Stop")){
  14.             //string -> int
  15.             //"10" -> Integer.parseInt("10")
  16.             int number = Integer.parseInt(command);
  17.             //сумираме числото
  18.             sum += number;
  19.             command = scanner.nextLine();
  20.             //прочетем нова команда
  21.         }
  22.  
  23.         System.out.println(sum);
  24.  
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement