Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class TreasureHunt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] initialLoot = scanner.nextLine().split("\\|");
- List<String> treasureChest = new ArrayList<>();
- for (String item : initialLoot) {
- treasureChest.add(item.trim());
- }
- String command = scanner.nextLine();
- while (!command.equals("Yohoho!")) {
- String[] tokens = command.split("\\s+");
- String action = tokens[0];
- switch (action) {
- case "Loot":
- for (int i = 1; i < tokens.length; i++) {
- String item = tokens[i];
- if (!treasureChest.contains(item)) {
- treasureChest.add(0, item);
- }
- }
- break;
- case "Drop":
- int index = Integer.parseInt(tokens[1]);
- if (index >= 0 && index < treasureChest.size()) {
- String droppedItem = treasureChest.remove(index);
- treasureChest.add(droppedItem);
- }
- break;
- case "Steal":
- int count = Integer.parseInt(tokens[1]);
- List<String> stolenItems = new ArrayList<>();
- int lastIndex = treasureChest.size() - 1;
- for (int i = 0; i < count; i++) {
- if (lastIndex >= 0) {
- stolenItems.add(treasureChest.remove(lastIndex));
- lastIndex--;
- }
- }
- for (int i = stolenItems.size() - 1; i >= 0; i--) {
- System.out.print(stolenItems.get(i));
- if (i > 0) {
- System.out.print(", ");
- }
- }
- System.out.println();
- break;
- }
- command = scanner.nextLine();
- }
- if (treasureChest.isEmpty()) {
- System.out.println("Failed treasure hunt.");
- } else {
- double totalLength = 0;
- for (String item : treasureChest) {
- totalLength += item.length();
- }
- double averageGain = totalLength / treasureChest.size();
- System.out.printf("Average treasure gain: %.2f pirate credits.%n", averageGain);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement