Advertisement
Guest User

Treasure Hunt

a guest
Jan 30th, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | Software | 0 0
  1. package _03Arrays_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class _10TreasureHunt {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String input = scanner.nextLine();
  10.         String[] treasures = input.split("\\|");
  11.         String stolenTreasures = "";
  12.  
  13.         String command = scanner.nextLine();
  14.         while(!command.equals("Yohoho!")) {
  15.             String[] treasureInput = command.split(" ");
  16.             String treasureCmd = treasureInput[0];
  17.  
  18.             if(treasures.length >= 2 && treasures.length <= 100) {
  19.                 if (treasureCmd.equals("Loot")) {
  20.                     for (int i = 1; i < treasureInput.length; i++) {
  21.                         boolean isEqual = false;
  22.  
  23.                         for (String treasure : treasures) {
  24.                             if (treasureInput[i].equals(treasure)) {
  25.                                 isEqual = true;
  26.                                 break;
  27.                             }
  28.                         }
  29.  
  30.                         if (!isEqual) {
  31.                             String[] treasuresNew = new String[treasures.length + 1];
  32.                             for (int j = 0; j < treasures.length; j++) {
  33.                                 treasuresNew[j] = treasures[j];
  34.                             }
  35.                             treasures = treasuresNew;
  36. //                      System.arraycopy(treasures, 0, treasuresNew, 0, treasures.length);
  37.  
  38.                             for (int j = 0; j < treasures.length; j++) {
  39.                                 if (j != treasures.length - 1) {
  40.                                     treasures[treasures.length - 1 - j] = treasures[treasures.length - 2 - j];
  41.                                 }
  42.                             }
  43.  
  44.                             treasures[0] = treasureInput[i];
  45.                         }
  46.                     }
  47.                 } else if (treasureCmd.equals("Drop")) {
  48.                     int index = Integer.parseInt(treasureInput[1]);
  49.  
  50.                     if (index >= 0 && index <= treasures.length - 1) {
  51.                         String droppedLoot = treasures[index];
  52.  
  53.                         for (int i = index; i < treasures.length - 1; i++) {
  54.                             treasures[i] = treasures[i + 1];
  55.                         }
  56.  
  57.                         treasures[treasures.length - 1] = droppedLoot;
  58.                     }
  59.                 } else if (treasureCmd.equals("Steal")) {
  60.                     int items = Integer.parseInt(treasureInput[1]);
  61.                     if (items >= 0 && items < treasures.length) {
  62.  
  63.                         for (int i = treasures.length - items; i < treasures.length; i++) {
  64.                             stolenTreasures += treasures[i];
  65.  
  66.                             if(i != treasures.length - 1) {
  67.                                 stolenTreasures += ", ";
  68.                             }
  69.                         }
  70.  
  71.                         String[] treasuresNew = new String[treasures.length - items];
  72.                         System.arraycopy(treasures, 0, treasuresNew, 0, treasuresNew.length);
  73.                         treasures = treasuresNew;
  74.                     } else {
  75.                         for (int i = 0; i < treasures.length; i++) {
  76.                             stolenTreasures += treasures[i];
  77.  
  78.                             if(i != treasures.length - 1) {
  79.                                 stolenTreasures += ", ";
  80.                             }
  81.                         }
  82.                         treasures = new String[0];
  83.                     }
  84.                 }
  85.             }
  86.             command = scanner.nextLine();
  87.         }
  88.  
  89.  
  90.         System.out.println(stolenTreasures);
  91.  
  92.         if(treasures.length > 0) {
  93.             int sumItems = 0;
  94.             for (String treasure : treasures) {
  95.                 sumItems += treasure.length();
  96.             }
  97.             double avgTreasureGain = sumItems * 1.0 / treasures.length;
  98.  
  99.             System.out.printf("Average treasure gain: %.2f pirate credits.", avgTreasureGain);
  100.         } else {
  101.             System.out.println("Failed treasure hunt.");
  102.         }
  103.     }
  104. }
  105.  
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement