Advertisement
CR7CR7

06task

Nov 13th, 2022
993
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LettersChangeNumbers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String[] codes = scanner.nextLine().split("\\s+");
  8.         double sum = 0;
  9.         for (String code : codes) {
  10.             double codeNumber = getCodeNumber(code);
  11.             sum += codeNumber;
  12.         }
  13.         System.out.printf("%.2f%n", sum);
  14.     }
  15.  
  16.     public static double getCodeNumber(String code) {
  17.         char firstLetter = code.charAt(0);
  18.         char lastLetter = code.charAt(code.length() - 1);
  19.         double number = Double.parseDouble(code.replace(firstLetter, ' ').replace(lastLetter, ' ').trim());
  20.  
  21.         if (Character.isUpperCase(firstLetter)) {
  22.             number /= (int) firstLetter - 64;
  23.         } else {
  24.             number *= (int) firstLetter - 96;
  25.         }
  26.  
  27.         if (Character.isUpperCase(lastLetter)) {
  28.             number -= (int) lastLetter - 64;
  29.         } else {
  30.             number += (int) lastLetter - 96;
  31.         }
  32.  
  33.         return number;
  34.     }
  35. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement