Advertisement
PePetrov96

Untitled

Aug 31st, 2022
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | Source Code | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.stream.Stream;
  4.  
  5. public class Task_10_Treasure_Hunt {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         String[] loot = scan.nextLine().split("\\|");
  9.         String input = scan.nextLine();
  10.  
  11.         while (!input.equals("Yohoho!")) {
  12.             String []commandLine = input.split(" ");
  13.             String commands = commandLine[0];
  14.             switch (commands) {
  15.                 case "Loot":
  16.                     String addResource = "";
  17.                     for (int i = 1; i < commandLine.length; i++) {
  18.                         boolean isUnique = true;
  19.                         for (String s : loot) {
  20.                             if (commandLine[i].equals(s)) {
  21.                                 isUnique = false;
  22.                             }
  23.                         }
  24.                         if (isUnique) {
  25.                             addResource += commandLine[i] + " ";
  26.                         }
  27.                     }
  28.                     String[] add = addResource.split(" ");
  29.                     loot = Stream.concat(Arrays.stream(add), Arrays.stream(loot)).toArray(String[] :: new);
  30.                     break;
  31.                 case "Drop":
  32.                     int Index = Integer.parseInt(commandLine[1]);
  33.                     if (Index >= 0 && Index < loot.length) {
  34.                         for (int i = Index; i < loot.length; i++) {
  35.                             loot[i] = loot[i + 1];
  36.                         }
  37.                         loot[loot.length - 1] = loot[Index];
  38.                     }
  39.                     break;
  40.                 case "Steal":
  41.                     String[] removeResource = new String[loot.length - 1];
  42.                     String removing = "";
  43.                     int remove = Integer.parseInt(commandLine[1]);
  44.                     if (loot.length < remove) {
  45.                         remove = loot.length;
  46.                     }
  47.                     for (int i = loot.length - 1; i > loot.length - remove; i--) {
  48.                         removing = loot[i] + " " + removing;
  49.                     }
  50.                     System.out.println(String.format(removing).replaceAll("[\\[|\\]]", ""));
  51.                     loot = removeResource;
  52.                     break;
  53.             }
  54.             input = scan.nextLine();
  55.         }
  56.         double sum = 0;
  57.         for (String s : loot) {
  58.             double size = s.length();
  59.             sum += size;
  60.         }
  61.         sum /= loot.length;
  62.         if (loot.equals(null)) {
  63.             System.out.println("Failed treasure hunt.");
  64.         } else {
  65.             System.out.printf("\nAverage treasure gain: %.2f pirate credits.", sum);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement