Advertisement
DNNdrago

2. Sum Cards

Jun 2nd, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _02_Problem2 {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.        
  9.         long value = 0l;
  10.         String input = sc.nextLine() + " e";
  11.         String[] cards = input.replaceAll("[SHDC]+", "").split(" ");
  12.        
  13.         int counter = 1;
  14.         for(int i = 0; i < cards.length - 1; i++) {
  15.             if (! cards[i].equals(cards[i + 1])) {
  16.                 if (counter != 1) {
  17.                     value += counter * getValue(cards[i]) * 2;
  18.                 }
  19.                 else {
  20.                     value += getValue(cards[i]);
  21.                 }
  22.                 counter = 1;
  23.             }
  24.             else {
  25.                 counter++;
  26.             }
  27.            
  28.         }
  29.        
  30.         System.out.println(value);
  31.        
  32.        
  33.        
  34.  
  35.     }
  36.  
  37.     public static int getValue(String card) {
  38.         int value = 0;
  39.         switch (card) {
  40.         case "J":
  41.             value = 12;
  42.             break;
  43.         case "Q":
  44.             value = 13;
  45.             break;
  46.         case "K":
  47.             value = 14;
  48.             break;
  49.         case "A":
  50.             value =  15;
  51.             break;
  52.  
  53.         default:
  54.             value = Integer.parseInt(card);
  55.             break;
  56.         }
  57.        
  58.         return value;
  59.     }
  60.    
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement