desislava_topuzakova

04. Sum of Chars

Sep 24th, 2021
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. package Test;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumOfChars_04 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. //прочитам -> взимаме ascii code -> сумираме
  9. int n = Integer.parseInt(scanner.nextLine()); //брой на символите
  10. int sum = 0; //сума от кодовете на символите
  11. for (int count = 1; count <= n; count++) {
  12. //1. прочитам символ
  13. char symbol = scanner.nextLine().charAt(0);
  14. //2. взимам аски кода
  15. //3. добавям аски кода към сумата
  16. sum += (int)symbol; //sum = sum + symbol
  17. }
  18.  
  19. System.out.println("The sum equals: " + sum);
  20. }
  21. }
  22.  
Advertisement
Add Comment
Please, Sign In to add comment