Advertisement
Dekameron

Untitled

May 24th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Locale;
  3. import java.util.Scanner;
  4.  
  5. public class _12_CardsFrequencies {
  6.  
  7.     public static void main(String[] args) {
  8.        
  9.         Locale.setDefault(Locale.ROOT);
  10.         Scanner sc = new Scanner(System.in);
  11.        
  12.         String[] allCards = sc.nextLine().split(" ");
  13.         ArrayList<String> outputCards = new ArrayList<>();
  14.         int[] cardsCounter = new int[13];
  15.         double[] percents = new double[13];
  16.         int allCardsCount = 0;
  17.        
  18.         for (int i = 0; i < allCards.length; i++) {         //Populate ArrayList with all cards
  19.             if(outputCards.contains(allCards[i])){          //without repeating
  20.                 continue;
  21.             }
  22.             allCards[i] = allCards[i].substring(0, allCards[i].length() - 1);
  23.             outputCards.add(allCards[i]);
  24.         }
  25.        
  26.         for(String card : allCards){                        //Counting every card
  27.            
  28.             if(card.contains("A")){
  29.                 cardsCounter[0]++;
  30.             } else if(card.contains("J")){
  31.                 cardsCounter[10]++;
  32.             } else if(card.contains("Q")){
  33.                 cardsCounter[11]++;
  34.             } else if(card.contains("K")){
  35.                 cardsCounter[12]++;
  36.             }
  37.             for (int i = 2; i <= 10; i++) {
  38.                 if(card.contains(i + "")){
  39.                     cardsCounter[i-1]++;
  40.                 }
  41.             }
  42.         }
  43.        
  44.         for(int count : cardsCounter){                  //Calculate the sum of all cards
  45.             allCardsCount += count;
  46.         }
  47.        
  48.         for (int i = 0; i < 13; i++) {                  //Calculate the percent of every card
  49.             if(cardsCounter[i] != 0){
  50.                 double helper = (double)cardsCounter[i]/allCardsCount;
  51.                 percents[i] = helper * 100;
  52.             }
  53.         }
  54.  
  55.         for (int i = 0, j = 0; i < 13; i++) {           //Matching the output card with the percent
  56.             if(cardsCounter[i] != 0){
  57.                 switch (outputCards.get(j)) {
  58.                 case "A":
  59.                     System.out.printf("A -> %.2f%% \n", percents[0]);
  60.                     break;
  61.                 case "2":
  62.                     System.out.printf("2 -> %.2f%% \n", percents[1]);
  63.                     break;
  64.                 case "3":
  65.                     System.out.printf("3 -> %.2f%% \n", percents[2]);
  66.                     break;
  67.                 case "4":
  68.                     System.out.printf("4 -> %.2f%% \n", percents[3]);
  69.                     break;
  70.                 case "5":
  71.                     System.out.printf("5 -> %.2f%% \n", percents[4]);
  72.                     break;
  73.                 case "6":
  74.                     System.out.printf("6 -> %.2f%% \n", percents[5]);
  75.                     break;
  76.                 case "7":
  77.                     System.out.printf("7 -> %.2f%% \n", percents[6]);
  78.                     break;
  79.                 case "8":
  80.                     System.out.printf("8 -> %.2f%% \n", percents[7]);
  81.                     break;
  82.                 case "9":
  83.                     System.out.printf("9 -> %.2f%% \n", percents[8]);
  84.                     break;
  85.                 case "10":
  86.                     System.out.printf("10 -> %.2f%% \n", percents[9]);
  87.                     break;
  88.                 case "J":
  89.                     System.out.printf("J -> %.2f%% \n", percents[10]);
  90.                     break;
  91.                 case "Q":
  92.                     System.out.printf("Q -> %.2f%% \n", percents[11]);
  93.                     break;
  94.                 case "K":
  95.                     System.out.printf("K -> %.2f%% \n", percents[12]);
  96.                     break;
  97.                 }
  98.                 j++;
  99.             }
  100.            
  101.         }
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement