Advertisement
Guest User

Letters Change Numbers

a guest
Jul 12th, 2019
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 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. String[] strings = scanner.nextLine().split("\\s+");
  7. double sum = 0;
  8.  
  9. for (String string : strings) {
  10. double number = Double.parseDouble(string.substring(1, string.length() - 1));
  11. if (number == 0) {
  12. System.out.println(0);
  13. return;
  14. }
  15.  
  16. char firstLetter = string.charAt(0);
  17. int firstLetterPosition = FindLetterPosition(firstLetter);
  18. if (isUpper(firstLetter)) {
  19. number /= firstLetterPosition;
  20.  
  21. } else {
  22. number *= firstLetterPosition;
  23. }
  24.  
  25. char lastLetter = string.charAt(string.length() - 1);
  26. int lastStringPosition = FindLetterPosition(lastLetter);
  27. if (isUpper(lastLetter)) {
  28. number -= lastStringPosition;
  29. } else {
  30. number += lastStringPosition;
  31. }
  32.  
  33. sum += number;
  34. }
  35. System.out.println(String.format("%.2f", sum));
  36. }
  37.  
  38.  
  39. private static boolean isUpper(char letter) {
  40. boolean isUpper = letter >= 65 && letter <= 90;
  41. return isUpper;
  42. }
  43.  
  44. private static int FindLetterPosition(char letter) {
  45. boolean isUpper = letter >= 65 && letter <= 90;
  46. int letterPositionAlfabet = 0;
  47.  
  48. if (isUpper) {
  49. return letterPositionAlfabet = letter - 65 + 1;
  50. } else {
  51. return letterPositionAlfabet = letter - 97 + 1;
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement