Advertisement
zh_stoqnov

LettersChangeNumbers

Feb 10th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LettersChangeNumbers {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. String[] nums = sc.nextLine().replaceAll("\\s+", " ").split(" ");
  8. double totalSum = 0;
  9. for(int i = 0; i < nums.length; i++) {
  10. String str = nums[i].replaceAll("[^-?0-9]+", "");
  11. double num = Double.parseDouble(str);
  12. if(isUpperCase(nums[i].charAt(0))) {
  13. num /= getValue(nums[i].charAt(0));
  14. } else {
  15. num *= getValue(nums[i].charAt(0));
  16. }
  17. if(isUpperCase(nums[i].charAt(nums[i].length() - 1))) {
  18. num -= getValue(nums[i].charAt(nums[i].length() - 1));
  19. } else {
  20. num += getValue(nums[i].charAt(nums[i].length() - 1));
  21. }
  22. totalSum += num;
  23. }
  24. System.out.printf("%.2f", totalSum);
  25. }
  26. public static boolean isLowerCase(char ch) {
  27. return ch >= 'a' && ch <= 'z';
  28. }
  29.  
  30. public static boolean isUpperCase(char ch) {
  31. return ch >= 'A' && ch <= 'Z';
  32. }
  33.  
  34. public static int getValue(char c) {
  35. int value = (int)c;
  36.  
  37. if (isUpperCase(c)) {
  38. value -= 64;
  39. }
  40. if (isLowerCase(c)) {
  41. value -= 96;
  42. }
  43. return value;
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement